java 集合知識整理,java集合知識

來源:互聯網
上載者:User

java 集合知識整理,java集合知識

 

 

  HashMap HashTable
 繼承方式

    
extends AbstractMap implements Map
extends Dictionary implements Map
安全執行緒 否 是(方法都是synchronized修飾)

效率相對比 高 低
允許有null的鍵和值 是 否
判斷包含的方法 containsvalue和containsKey contains

hash數組預設大小

11

 

16

 

hash數組增加方式

 old*2+1

2的指數增加

 

 1         List<String> list = new ArrayList<String>(); 2  3         String preString = "aa"; 4         for (int j = 0; j < 100000000; j++) { 5             list.add(preString); 6         } 7  8         // 方法1(速度最快,List特有的) 9         for (int i = 0, len = list.size(); i < len; i++) {10             list.get(i);11         }12 13         // 方法2 (for each-最耗時)14         for (String tmp : list) {15         }16 17         // 方法3(與方法2,4一樣,適用所有實現了Iterable介面的類,常見的有:Queue,Set,Collection,List)18         Iterator<String> iter = list.iterator();19         while (iter.hasNext()) {20             String str = iter.next();21         }22     23         // 方法424         for (Iterator<String> it2 = list.iterator(); it2.hasNext();) {25             String str = it2.next();26         }

 

    2.map的遍曆

 

 1         HashMap<Integer, String> map = new HashMap<>(); 2         String v = "value"; 3         for(int i=0;i<10000000;i++){ 4             map.put(i, v); 5         } 6          7         //方法1, 8         Iterator<Map.Entry<Integer, String>> it1= map.entrySet().iterator(); 9         while (it1.hasNext()) {10             Map.Entry<Integer, String> entry =it1.next();11             int key = entry.getKey();12             String value = entry.getValue();13         }14     15         //方法2 擷取map的key集合的迭代器,耗時是方法1的7倍。16         Iterator<Integer> it2 = map.keySet().iterator();17         while(it2.hasNext()){18             int key = it2.next();19             String value = map.get(key);20         }21     

 

 set 和map的遍曆方式類似,就不在這裡囉嗦了。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.