集合(雙列)

來源:互聯網
上載者:User

標籤:iter   儲存   預設   htable   訪問速度   使用   key   映射   cti   

Map

  HashMap

    LinkedHashMap

  Hashtable

  TreeMap

 

Map用於儲存索引值對,根據鍵得到值,因此不允許鍵重複,值可以重複。
l  (1)HashMap是一個最常用的Map,它根據鍵的hashCode值儲存資料,根據鍵可以直接擷取它的值,具有很快的訪問速度。HashMap最多隻允許一條記錄的鍵為null,不允許多條記錄的值為null。HashMap不支援線程的同步,即任一時刻可以有多個線程同時寫HashMap,可能會導致資料的不一致。如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。
l  (2)Hashtable與HashMap類似,不同的是:它不允許記錄的鍵或者值為空白;它支援線程的同步,即任一時刻只有一個線程能寫Hashtable,然而,這也導致了Hashtable在寫入時會比較慢。
l  (3)LinkedHashMap儲存了記錄的插入順序,在用Iteraor遍曆LinkedHashMap時,先得到的記錄肯定是先插入的。在遍曆的時候會比HashMap慢。有HashMap的全部特性。
l  (4)TreeMap能夠把它儲存的記錄根據鍵排序,預設是按升序排序,也可以指定排序的比較子。當用Iteraor遍曆TreeMap時,得到的記錄是排過序的。TreeMap的鍵和值都不可為空。

TreeMap取出來的是排序後的索引值對。但如果您要按自然順序或自訂順序遍曆鍵,那麼TreeMap會更好。

Map的四種遍曆方式:
 1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.Map; 4  5 public class TestMap { 6     public static void main(String[] args) { 7         Map<Integer, String> map = new HashMap<Integer, String>(); 8         map.put(1, "a"); 9         map.put(2, "b");10         map.put(3, "ab");11         map.put(4, "ab");12         map.put(4, "ab");// 和上面相同 , 會自己篩選13         System.out.println(map.size());14         // 第一種:15         /*16          * Set<Integer> set = map.keySet(); //得到所有key的集合17          * 18          * for (Integer in : set) { String str = map.get(in);19          * System.out.println(in + "     " + str); }20          */21         System.out.println("第一種:通過Map.keySet遍曆key和value:");22         for (Integer in : map.keySet()) {23             //map.keySet()返回的是所有key的值24             String str = map.get(in);//得到每個key多對用value的值25             System.out.println(in + "     " + str);26         }27         // 第二種:28         System.out.println("第二種:通過Map.entrySet使用iterator遍曆key和value:");29         Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();30         while (it.hasNext()) {31              Map.Entry<Integer, String> entry = it.next();32                System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());33         }34         // 第三種:推薦,尤其是容量大時35         System.out.println("第三種:通過Map.entrySet遍曆key和value");36         for (Map.Entry<Integer, String> entry : map.entrySet()) {37             //Map.entry<Integer,String> 映射項(鍵-值對)  有幾個方法:用上面的名字entry38             //entry.getKey() ;entry.getValue(); entry.setValue();39             //map.entrySet()  返回此映射中包含的映射關係的 Set視圖。40             System.out.println("key= " + entry.getKey() + " and value= "41                     + entry.getValue());42         }43         // 第四種:44         System.out.println("第四種:通過Map.values()遍曆所有的value,但不能遍曆key");45         for (String v : map.values()) {46             System.out.println("value= " + v);47         }48     }49 }

 

集合(雙列)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.