Java Map介面及其實現類原理解析
Map介面
Map提供了一種對映關係,其中的元素是以鍵值對(key-value)的形式儲存的,能夠實現根據key快速查詢value;
Map中的鍵值對以Entry型別的物件例項形式存在;
建(key值)不可重複,value值可以重複,一個value值可以和很多key值形成對應關係,每個建最多隻能對映到一個值。
Map支援泛型,形式如:Map<K,V>
Map中使用put(K key,V value)方法新增
Map介面中定義的常用方法
具體使用在實現類中討論
int size();//獲取Map集合大小(即元素數量) boolean isEmpty();//判斷是否為空 boolean containsKey(Object key);//判斷是否包含某個鍵 boolean containsValue(Object value);//判斷是否包含某個值 V get(Object key);//獲取某個鍵對應的值 V put(K key,V value);//新增鍵值對(K,V) V remove(Object key);//移除某個鍵對應的鍵值對 void putAll(Map<? extends K,? extends V> m);//新增另一個Map集合 void clear();//清空所有鍵值對 Set<K> keySet();//獲取鍵的集合 Collection<V> values();//獲取值的集合 Set<Map.Entry<K,V>> entrySet();//獲取鍵值對實體的集合 interface Entry<K,V>//Map中的內部介面
HashMap
基於雜湊表的 Map 介面的實現。此實現提供所有可選的對映操作,並允許使用 null 值和 null 鍵。(除了非同步和允許使用 null 之外,HashMap 類與 Hashtable 大致相同。)除實現了Map介面外還實現了Cloneable,Serializable,繼承了AbstractMap抽象類
此類不保證對映的順序,特別是它不保證該順序恆久不變。
特點:
- 鍵無序,唯一,類似於Set集合
- 值有序,可重複,類似於List
- 底層資料結構是雜湊表,保證鍵唯一
允許鍵為null,值為null
// HashMap<String,Student> hm = new HashMap<String,Student>(); // hm.put("2018050401",new Student("2018050401","張三",18,80.0)); // hm.put("2018050402",new Student("2018050402","李四",80.0)); // hm.put("2018050403",new Student("2018050403",80.0)); // hm.put("2018050404",new Student("2018050404","王五",80.0)); // // // 方式一: 通過鍵找值 // Set<String> keys = hm.keySet(); // for (String key : keys) { // Student s = hm.get(key); // System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore()); // } HashMap<Student,String> hm = new HashMap<Student,String>(); hm.put(new Student("2018050401",80.0),"2018050401"); hm.put(new Student("2018050402","2018050402"); hm.put(new Student("2018050403","2018050403"); hm.put(new Student("2018050404","2018050404"); hm.put(new Student("2018050404","2018050404"); // 方式二: 通過鍵值對物件找鍵找值 Set<Entry<Student,String>> keyValues = hm.entrySet(); for (Entry<Student,String> keyValue : keyValues) { Student s = keyValue.getKey(); String value = keyValue.getValue(); System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value); }
LinkedHashMap
Map 介面的雜湊表和連結串列實現,具有可預知的迭代順序
特點:
- 鍵有序,唯一,
- 值有序,可重複,類似於List
底層資料結構是雜湊表和連結串列,雜湊表保證鍵唯一,連結串列保證鍵有序
LinkedHashMap<Integer,String> lhm = new LinkedHashMap<Integer,String>(); lhm.put(01,"張三1"); lhm.put(02,"張三2"); lhm.put(03,"張三3"); lhm.put(04,"張三4"); lhm.put(05,"張三5"); Set<Integer> keys = lhm.keySet(); for (Integer key : keys) { System.out.println(key + "|" + lhm.get(key)); }
TreeMap
基於紅黑樹(Red-Black tree)的 NavigableMap 實現。該對映根據其鍵的自然順序進行排序,或者根據建立對映時提供的 Comparator 進行排序,
具體取決於使用的構造方法。
特點:
- 鍵可排序,唯一,
- 值有序,可重複,類似於List
- 底層資料結構是自平衡的二叉樹,可排序
排序方式類似於TreeSet,分為自然排序和比較器排序,具體取決於使用的構造方法
TreeMap<Integer,String> tm = new TreeMap<Integer,String>(); tm.put(24,"Hello1"); tm.put(14,"Hello2"); tm.put(34,"Hello3"); tm.put(124,"Hello4"); tm.put(24,"Hello5"); tm.put(24,"Hello6"); tm.put(24,"Hello7"); tm.put(244,"Hello8"); tm.put(624,"Hello9"); tm.put(24,"Hello10"); Set<Integer> keys = tm.keySet(); for (Integer key : keys) { String value = tm.get(key); System.out.println(key + "|" + value); }
HashTable
此類實現一個雜湊表,該雜湊表將鍵對映到相應的值。任何非 null 物件都可以用作鍵或值
特點:
- 不允許null鍵和null值
- 執行緒安全,效率低
HashMap和Hashtable的區別:
- HashMap是不安全的不同步的效率高的 允許null鍵和null值
- Hashtable是安全的同步的效率低的 不允許null鍵和null值
底層都是雜湊表結構
Hashtable<String,String> hashtable = new Hashtable<String,String>(); hashtable.put("劉備","孫尚香"); hashtable.put("孫策","大喬"); hashtable.put("周瑜","小喬"); hashtable.put("呂布","貂蟬"); System.out.println(hashtable); Enumeration<String> keys = hashtable.keys(); while (keys.hasMoreElements()) { String key = keys.nextElement(); String value = hashtable.get(key); System.out.println(key + "|" + value); }
WeakHashMap
以弱鍵 實現的基於雜湊表的 Map。在 WeakHashMap 中,當某個鍵不再正常使用時,將自動移除其條目。更精確地說,對於一個給定的鍵,其對映的存在並不阻止垃圾回收器對該鍵的丟棄,這就使該鍵成為可終止的,被終止,然後被回收。
丟棄某個鍵時,其條目從對映中有效地移除,因此,該類的行為與其他的 Map 實現有所不同。
WeakHashMap<String,String> whm = new WeakHashMap<>(); whm.put(new String("hello1"),"world1"); whm.put(new String("hello2"),"world2"); whm.put(new String("hello3"),"world3"); whm.put("hello4","world3"); System.out.println(whm); System.gc(); System.runFinalization(); System.out.println(whm);
鍵是列舉型別
EnumMap<Direction,String> em = new EnumMap<>(Direction.class); em.put(Direction.UP,"向上移動"); em.put(Direction.DOWN,"向下移動"); em.put(Direction.LEFT,"向左移動"); em.put(Direction.RIGHT,"向右移動"); Set<Direction> keys = em.keySet(); for (Direction key : keys) { String value = em.get(key); System.out.println(key + "|" + value); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。