關於集合子類父類關係的全面總結 ,以及遍歷方法的總結 全
集合
集合是個非常好的儲存結構,你補了陣列不能改變長度的不足。可以儲存在臨時記憶體的資料的載體。
集合有兩大父類,分別是Collection 和 Map
1.Collection ——Set——HashSet (不是鍵值對,不安全,不能有重複資料,沒有順序)
——Queue——LinkedList (適合進行插入刪除 ,因為內部是以連結串列實現的)
——List——ArrayList(執行緒不安全 ) \ __ 適合進行隨機遍歷,因為是陣列形式實現的。
——Vector(執行緒安全) /
2.Map ———HashMap——LinkHashMap 區別於hashtable:執行緒安全的 沒有null鍵和值 父類是dictionary
———HashTable——Properties (通常與對排序進行合作,進行海量資料的塊數查詢) 區別於hashMap :執行緒不安全的 有null鍵和值 父類是abstractmap
———SortedMap——TreeMap
注:hashmap和hashtable 在定位儲存完成後也可以進行排序(主要是通過封裝到list集合中進行自定義排序):List<> list=new ArrayList<>(t.entrySet();) Collections.sort(list,new COnparator(){ 重寫 compare方法}),排序完list之後遍歷list集合就行了。
注:hash和list的區別:hash是無序的,並且不能重複的。而list是有序的,可以重複的。
注:hash 演算法實質。(就是在要求物件不相同的情況下,hash演算法可以大大降低呼叫equals方法的次數。)直接定位,如果定位上有元素才呼叫equals方法進行比較,存 儲或覆蓋。
list集合的5種遍歷方法
1.iterator遍歷 Iterator<String> it=list.iterator(); while(it.hasNext){String e=it.next();}
2.foreach遍歷 for(String s:list){System.out.println(s);}
3.ListIterator遍歷 ListIterator<String> ls=list.listIterator(); while(ls.hasNext){String e=ls.next();} 注:LIstIterator 遍歷是可以雙向的previous()和hasPrevious()方法 就可以實現。
4.正常for迴圈遍歷 for(int i=0;i<list.size();i++){System.out.println(list.get(i));} 注:同樣的for迴圈也可以實現對list的雙向遍歷。
5.toArray() 方法遍歷 Object []obj=list.toArray(); for(int i=0;i<os.length;i++){System.out.println(os[i]);} 注:同樣的道理 ,toArray方法也可以實現雙向遍歷。
Hashtable集合的5種遍歷方法
1 根據值來遍歷 :.Enumeration es=t.elements(); while(es.hasMoreElements()){System.out.println(es.nextElement();)}
2.根據鍵來遍歷: Enumeration es=t.keys(); while(es.hasMoreElements()){System.out.println(t.get(es.nextElement()));}
3.根據返回值是collection來進行遍歷: Collection cs=t.values();for(Interger i:cs){System.out.println(i)};
4.根據返回值是Set集合來進行遍歷:Set ss=t.keySet(); Iterator ot=ss.iterator(); while(it.hasnext()){System.out.println(t.get(it.next))};
5.根據返回值是Entry集合進行遍歷: Set<Map.Entry<Sring,Integer>> ts.entrySet();Iterator<Map.Entry<String,Integer>> it2=ts.iteraator();while(it2.hasNext()) {Entry<String,Integer> e=it2.next(); System.out.println(e.getValue());};