Collections集合工具類:
shuffle() 隨機置換打亂(只能傳list)
sort() 集合元素升序排列
import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; public class Demo05 { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); System.out.println(list); //打亂集合元素順序 Collections.shuffle(list); System.out.println(list); //排序(按內容排序) Collections.sort(list); System.out.println(list); } }
集合嵌套:
集合嵌套,僅僅是集合內容又是集合,如Collection集合嵌套、Collection集合與Map集合相互嵌套、Map集合嵌套。
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Demo06 { public static void main(String[] args) { //大 Map HashMap<String, HashMap<Person,String>> oracle=new HashMap<String, HashMap<Person,String>>(); //小Map HashMap<Person,String> java1018= new HashMap<Person,String>(); HashMap<Person,String> java1227= new HashMap<Person,String>(); //封裝小的 java1018.put(new Person("吉吉國王",18), "山東淄博"); java1018.put(new Person("熊大",18), "山東濟南"); java1227.put(new Person("熊二",28), "山東濟南"); java1227.put(new Person("光頭強",20), "山東淄博"); //封裝大的 oracle.put("java1018", java1018); oracle.put("java1227", java1227); //entrySet+Iterator //先獲取大Map的結婚證對象Set集合 Set<Map.Entry<String, HashMap<Person,String>>> bigentrys=oracle.entrySet(); //2.遍歷結婚證集合到每一個結婚證對象 //獲取迭代器對象 Iterator<Map.Entry<String, HashMap<Person,String>>> it=bigentrys.iterator(); while(it.hasNext()){ //獲取每一個大結婚證對象 Map.Entry<String, HashMap<Person,String>> bigentry=it.next(); //獲取大結婚證對象的key String bigkey=bigentry.getKey(); //獲取大結婚證對象的value HashMap<Person,String> smallMap=bigentry.getValue(); //3.獲取小Map的結婚證對象Set集合 Set<Map.Entry<Person,String>> smallentrys=smallMap.entrySet(); //4.遍歷小結婚證對象取到每一個小結婚證對象 //獲取迭代器對象 Iterator<Map.Entry<Person,String>> it2=smallentrys.iterator(); while(it2.hasNext()){ //獲取每一個小結婚證對象 Map.Entry<Person,String> smallentry=it2.next(); //獲取小結婚證對象的key Person smallKey=smallentry.getKey(); //獲取小結婚證對象的value String smallValue=smallentry.getValue(); System.out.println(bigkey+"..."+smallKey+"..."+smallValue); } } //keySet+增強for Set<String> set1=oracle.keySet(); for(String s:set1){ //小map value HashMap<Person, String> map = oracle.get(s); Set<Person> s1=map.keySet(); for(Person s2:s1){ System.out.println(s+"..."+s2+"..."+map.get(s2)); } } System.out.println("---------keySet+iteracor"); Set<String> set2=oracle.keySet(); Iterator<String> it=set2.iterator(); while(it.hasNext()){ String p=it.next(); HashMap<Person, String> map = oracle.get(p); Set<Person> s3=map.keySet(); Iterator<Person> it2=s3.iterator(); while(it2.hasNext()){ Person p1=it2.next(); System.out.println(p+"..."+p1+"..."+map.get(p1)); } } System.out.println("---------entrySet+增強for"); Set<Map.Entry<String, HashMap<Person,String>>> bigentrys=oracle.entrySet(); for(Map.Entry<String, HashMap<Person,String>> s4:bigentrys){ String s5 = s4.getKey(); HashMap<Person,String> map2=s4.getValue(); Set<Map.Entry<Person,String>> s6=map2.entrySet(); for(Map.Entry<Person,String> s7:s6){ System.out.println(s5+"..."+s7.getKey()+"..."+s7.getValue()); } } } }
靜態導入:
在導包的過程中我們可以直接導入靜態部分,這樣某個類的靜態成員就可以直接使用了。在源碼中經常會出現靜態導入。
靜態導入格式:
import static XXX.YYY; 導入后YYY可直接使用。
可變參數:
在JDK1.5之后,如果我們定義一個方法需要接受多個參數,並且多個參數類型一致,我們可以對其簡化成如下格式:
修飾符 返回值類型 方法名(參數類型... 形參名){ }
其實這個書寫完全等價與
修飾符 返回值類型 方法名(參數類型[] 形參名){ }
只是后面這種定義,在調用時必須傳遞數組,而前者可以直接傳遞數據即可。