java集合類

轉載:http://www.cnblogs.com/hzmark/archive/2012/12/17/CollectionBase.html  

數組是一種很常見的數據結構,開始接觸編程的時候多數程序都和數組相關。剛開始接觸Java時也是一直使用數組寫一些程序,後來越來越覺得數組這東西沒法滿足需求了,這時一位“前輩”對我說了一句:不會用集合類就等於沒學過Java。然後才知道有集合類。

    想想已經是3、4年前的事了,時間如白駒過隙啊。

    什麼時候數組會顯得力不從心,沒法滿足需求,需要集合類呢?

  1. 不知道具體數據長度
  2. 需要自動排序
  3. 存儲鍵值對

    當然,上面的情況不是絕對的,只是數組比較難滿足。這時集合類(也可稱爲容器類)就顯示了它強大的功能。

    集合類的分類(圖片轉自http://biancheng.dnbcw.info/1000wen/359774.html)

    

    上圖中不包含Queue內容,部分Map的實現類未給出。

    常見使用的有List、Set、Map及他們的實現類。

    List、Set、Map接口及各實現類的特性

接口

特性

實現類

實現類特性

成員要求

List

線性、有序的存儲容器,可通過索引訪問元素

ArrayList

數組實現。非同步。

 

Vector

類似ArrayList,同步。

 

LinkedList

雙向鏈表。非同步。

 

Map

保存鍵值對成員

HashMap

基於哈希表的 Map 接口的實現,滿足通用需求

任意Object對象,如果修改了equals方法,需同時修改hashCode方法

TreeMap

默認根據自然順序進行排序,或者根據創建映射時提供的Comparator進行排序

鍵成員要求實現caparable接口,或者使用Comparator構造TreeMap。鍵成員一般爲同一類型。

LinkedHashMap

類似於HashMap,但迭代遍歷時取得“鍵值對”的順序是其插入順序或者最近最少使用的次序

與HashMap相同

IdentityHashMap

使用==取代equals()對“鍵值”進行比較的散列映射

成員通過==判斷是否相等

WeakHashMap

弱鍵映射,允許釋放映射所指向的對象

 

ConcurrentHashMap

線性安全的Map

 

Set

成員不能重複

HashSet

爲快速查找設計的Set

元素必須定義hashCode()

TreeSet

保持次序的Set,底層爲樹結構

元素必須實現Comparable接口

LinkedHashSet

內部使用鏈表維護元素的順序(插入的次序)

元素必須定義hashCode()

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   

 

 

 

 

 

    在滿足要求的情況下,Map應儘量使用HashMap,Set應儘量使用HashSet。

    集合類的基本使用

    List

 

  1. List基本操作 
  2.  ArrayList<String> arrayList = new ArrayList<String>(); 
  3.          arrayList.add("Tom"); 
  4.          arrayList.add("Jerry"); 
  5.          arrayList.add("Micky"); 
  6.          // 使用Iterator遍歷元素 
  7.          Iterator<String> it = arrayList.iterator(); 
  8.          while (it.hasNext()) { 
  9.              String str = it.next(); 
  10.              System.out.println(str); 
  11.          } 
  12.          // 在指定位置插入元素 
  13.          arrayList.add(2"Kate"); 
  14.          // 通過索引直接訪問元素 
  15.          for (int i = 0; i < arrayList.size(); i++) { 
  16.              System.out.println(arrayList.get(i)); 
  17.          } 
  18.          List<String> subList = new ArrayList<String>(); 
  19.          subList.add("Mike"); 
  20.          // addAll(Collection<? extends String> c)添加所給集合中的所有元素 
  21.          arrayList.addAll(subList); 
  22.          // 判斷是否包含某個元素 
  23.          if (arrayList.contains("Mike")) { 
  24.              System.out.println("Mike is include in the list"); 
  25.          } 
  26.   
  27.          LinkedList<String> linkedList = new LinkedList<String>(); 
  28.          linkedList.addAll(arrayList); 
  29.          // 獲取指定元素 
  30.          System.out.println(linkedList.get(4)); 
  31.          // 獲取第一個元素 
  32.          System.out.println(linkedList.getFirst()); 
  33.          // 獲取最後一個元素 
  34.          System.out.println(linkedList.getLast()); 
  35.          // 獲取並刪除第一個元素 
  36.          System.out.println(linkedList.pollFirst()); 
  37.          // 獲取,但不移除第一個元素 
  38.          System.out.println(linkedList.peekFirst()); 

ArrayList和LinkedList的效率比較

 

  1. ArrayList添加元素的效率 
  2.  ArrayList<String> arrList = new ArrayList<String>(); 
  3.          long startTimeMillis, endTimeMillis; 
  4.          startTimeMillis = System.currentTimeMillis(); 
  5.          for (int i = 0; i < 10000; i++) { 
  6.              arrList.add(0"addString"); 
  7.          } 
  8.          endTimeMillis = System.currentTimeMillis(); 
  9.          System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 
  10.                  + "ms"); 
  11.   
  12.          arrList.clear(); 
  13.   
  14.          startTimeMillis = System.currentTimeMillis(); 
  15.          for (int i = 0; i < 20000; i++) { 
  16.              arrList.add(0"addString"); 
  17.          } 
  18.          endTimeMillis = System.currentTimeMillis(); 
  19.          System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 
  20.                  + "ms"); 
  21.   
  22.          arrList.clear(); 
  23.   
  24.          startTimeMillis = System.currentTimeMillis(); 
  25.          for (int i = 0; i < 40000; i++) { 
  26.              arrList.add(0"addString"); 
  27.          } 
  28.          endTimeMillis = System.currentTimeMillis(); 
  29.          System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 
  30.                  + "ms"); 
  31.   
  32.          arrList.clear(); 
  33.   
  34.          startTimeMillis = System.currentTimeMillis(); 
  35.          for (int i = 0; i < 80000; i++) { 
  36.              arrList.add(0"addString"); 
  37.          } 
  38.          endTimeMillis = System.currentTimeMillis(); 
  39.          System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 
  40.                  + "ms"); 
  41.   
  42.          arrList.clear(); 
  43.   
  44.          startTimeMillis = System.currentTimeMillis(); 
  45.          for (int i = 0; i < 160000; i++) { 
  46.              arrList.add(0"addString"); 
  47.          } 
  48.          endTimeMillis = System.currentTimeMillis(); 
  49.          System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 
  50.                  + "ms"); 
  51.   
  52.          arrList.clear(); 
  53.   
  54.          startTimeMillis = System.currentTimeMillis(); 
  55.          for (int i = 0; i < 320000; i++) { 
  56.              arrList.add(0"addString"); 
  57.          } 
  58.          endTimeMillis = System.currentTimeMillis(); 
  59.          System.out.println("ArrayList:" + (endTimeMillis - startTimeMillis) 
  60.                  + "ms"); 

 

執行時

  1. Set基礎操作 
  2.             List<Integer> list = new ArrayList<Integer>(); 
  3.             list.add(3); 
  4.             list.add(4); 
  5.             HashSet<Integer> hashSet = new HashSet<Integer>(); 
  6.             hashSet.add(1); 
  7.             hashSet.add(3); 
  8.             hashSet.add(2); 
  9.             hashSet.add(6); 
  10.             // 重複元素將不能被添加 
  11.             hashSet.add(3); 
  12.             // 只要有元素被添加就返回true 
  13.             if (hashSet.addAll(list)) { 
  14.                 System.out.println("Add success"); 
  15.             } 
  16.             // 判斷是否存在某個集合 
  17.             if (hashSet.containsAll(list)) { 
  18.                 System.out.println("The hashSet is contain 3 and 4"); 
  19.             } 
  20.             Iterator<Integer> it = hashSet.iterator(); 
  21.             while (it.hasNext()) { 
  22.                 System.out.print(it.next() + " "); 
  23.                 // 1 2 3 4 6 
  24.                 // 看結果是被排序了,HashSet按照Hash函數排序,Integer值的HashCode就是其int值 
  25.             } 
  26.             // 換轉成數組 
  27.             Object[] integers = hashSet.toArray(); 
  28.             for (int i = 0; i < integers.length; i++) { 
  29.                 System.out.print((Integer) integers[i]); 
  30.             } 
  31.             //移除元素 
  32.             hashSet.remove(3); 
  33.              
  34.             TreeSet<String> treeSet = new TreeSet<String>(); 
  35.             treeSet.add("C"); 
  36.             treeSet.add("A"); 
  37.             treeSet.add("D"); 
  38.             treeSet.add("B"); 
  39.             for (Iterator<String> strIt = treeSet.iterator(); strIt.hasNext();) { 
  40.                 System.out.print(strIt.next()); 
  41.                 // ABCD 按照字母順序 
  42.             } 
  43.             LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>(); 
  44.             linkedHashSet.add("C"); 
  45.             linkedHashSet.add("A"); 
  46.             linkedHashSet.add("D"); 
  47.             linkedHashSet.add("B"); 
  48.             for (Iterator<String> linkedIt = linkedHashSet.iterator(); linkedIt 
  49.                     .hasNext();) { 
  50.                 System.out.print(linkedIt.next()); 
  51.                 // CADB 按照插入順序 
  52.             } 

間比較

執行次數(在0號位置插入)

ArrayList所用時間(ms)

LinkedList所用時間(ms)

10000

31

0

20000

141

0

40000

484

16

80000

1985

0

160000

7906

0

320000

31719

16

執行次數(在尾部插入)

ArrayList所用時間(ms)

LinkedList所用時間(ms)

10000

0

0

20000

15

0

40000

0

0

80000

0

0

160000

0

15

320000

0

16

循環輸出次數(get(index)方法)

ArrayList所用時間(ms)

LinkedList所用時間(ms)

10000

93

204

20000

188

797

40000

328

2734

80000

688

13328

160000

1594

62313

320000

2765

太久了……

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

   

 

 

 

    因爲ArrayList底層由數組實現,在0號位置插入時將移動list的所有元素,在末尾插入元素時不需要移動。LinkedList是雙向鏈表,在任意位置插入元素所需時間均相同。所以在List中有較多插入和刪除操作的情況下應使用LinkedList來提高效率,而有較多索引查詢的時候使用ArrayList(使用增強型的for循環或Iterator遍歷LinkedList效率將提高很多)。

    Map

  1. Map基本操作 
  2.  HashMap<String, Integer> map = new HashMap<String, Integer>(); 
  3.          // 向Map中添加元素 
  4.          map.put("Tom"26); 
  5.          map.put("Jack"18); 
  6.          map.put("Micky"17); 
  7.          map.put("Kate"15); 
  8.          // 根據Key獲取Value 
  9.          System.out.println("Jack is " + map.get("Jack") + " years old"); 
  10.          // 移除 
  11.          map.remove("Micky"); 
  12.          // 遍歷Map 
  13.          for (Entry<String, Integer> entry : map.entrySet()) { 
  14.              System.out.println("name:" + entry.getKey() + " age:" 
  15.                      + entry.getValue()); 
  16.          } 
  17.          // Key相同的元素將被覆蓋 
  18.          map.put("Jack"19); 
  19.          // 根據Key獲取Value 
  20.          System.out.println("Jack is " + map.get("Jack") + " years old"); 
  21.          // 判斷是否包含某個Key 
  22.          if (map.containsKey("Tom")) { 
  23.              System.out.println(map.get("Tom")); 
  24.          } 
  25.          // 判斷是否包含某個Value 
  26.          if (map.containsValue(26)) { 
  27.              System.out.println("The map include the value 26"); 
  28.          } 
  29.          // 判斷map是否爲空 
  30.          if (!map.isEmpty()) { 
  31.              // 獲取map大小 
  32.              System.out.println("The map's size=" + map.size()); 
  33.          } 
  34.          // 獲取Key的集合 
  35.          for (String str : map.keySet()) { 
  36.              System.out.println(str); 
  37.          } 
  38.   
  39.          TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>(); 
  40.          treeMap.putAll(map); 
  41.          // 輸出內容按照key值排序 
  42.          for (Entry<String, Integer> entry : treeMap.entrySet()) { 
  43.              System.out.println("name:" + entry.getKey() + " age:" 
  44.                      + entry.getValue()); 
  45.              // name:Jack age:19 
  46.              // name:Kate age:15 
  47.              // name:Tom age:26 
  48.          } 
  49.   
  50.          LinkedHashMap<String, Integer> linkedHashMap = new LinkedHashMap<String, Integer>(); 
  51.          // 向Map中添加元素 
  52.          linkedHashMap.put("Tom"26); 
  53.          linkedHashMap.put("Jack"18); 
  54.          linkedHashMap.put("Micky"17); 
  55.          linkedHashMap.put("Kate"15); 
  56.          // 保持了插入的順序 
  57.          for (Entry<String, Integer> entry : linkedHashMap.entrySet()) { 
  58.              System.out.println("name:" + entry.getKey() + " age:" 
  59.                      + entry.getValue()); 
  60.              // name:Tom age:26 
  61.              // name:Jack age:18 
  62.              // name:Micky age:17 
  63.              // name:Kate age:15 
  64.          } 

 

Set

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章