Java Map接口及其實現(xiàn)類原理解析
Map接口
Map提供了一種映射關(guān)系,其中的元素是以鍵值對(key-value)的形式存儲的,能夠?qū)崿F(xiàn)根據(jù)key快速查找value;
Map中的鍵值對以Entry類型的對象實例形式存在;
建(key值)不可重復(fù),value值可以重復(fù),一個value值可以和很多key值形成對應(yīng)關(guān)系,每個建最多只能映射到一個值。
Map支持泛型,形式如:Map<K,V>
Map中使用put(K key,V value)方法添加
Map接口中定義的常用方法
具體使用在實現(xiàn)類中討論
int size();//獲取Map集合大小(即元素數(shù)量) boolean isEmpty();//判斷是否為空 boolean containsKey(Object key);//判斷是否包含某個鍵 boolean containsValue(Object value);//判斷是否包含某個值 V get(Object key);//獲取某個鍵對應(yīng)的值 V put(K key, V value);//添加鍵值對(K,V) V remove(Object key);//移除某個鍵對應(yīng)的鍵值對 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中的內(nèi)部接口
HashMap
基于哈希表的 Map 接口的實現(xiàn)。此實現(xiàn)提供所有可選的映射操作,并允許使用 null 值和 null 鍵。(除了非同步和允許使用 null 之外,HashMap 類與 Hashtable 大致相同。)除實現(xiàn)了Map接口外還實現(xiàn)了Cloneable,Serializable,繼承了AbstractMap抽象類
此類不保證映射的順序,特別是它不保證該順序恒久不變。
特點:
- 鍵無序,唯一,類似于Set集合
- 值有序,可重復(fù),類似于List
- 底層數(shù)據(jù)結(jié)構(gòu)是哈希表,保證鍵唯一
允許鍵為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", "李四", 18, 80.0)); // hm.put("2018050403", new Student("2018050403", "李四", 18, 80.0)); // hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0)); // hm.put("2018050404", new Student("2018050404", "王五", 18, 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", "張三", 18, 80.0),"2018050401"); hm.put(new Student("2018050402", "李四", 18, 80.0),"2018050402"); hm.put(new Student("2018050403", "李四", 18, 80.0), "2018050403"); hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404"); hm.put(new Student("2018050404", "王五", 18, 80.0), "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 接口的哈希表和鏈表實現(xiàn),具有可預(yù)知的迭代順序
特點:
- 鍵有序,唯一,
- 值有序,可重復(fù),類似于List
底層數(shù)據(jù)結(jié)構(gòu)是哈希表和鏈表,哈希表保證鍵唯一,鏈表保證鍵有序
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 實現(xiàn)。該映射根據(jù)其鍵的自然順序進行排序,或者根據(jù)創(chuàng)建映射時提供的 Comparator 進行排序,
具體取決于使用的構(gòu)造方法。
特點:
- 鍵可排序,唯一,
- 值有序,可重復(fù),類似于List
- 底層數(shù)據(jù)結(jié)構(gòu)是自平衡的二叉樹,可排序
排序方式類似于TreeSet,分為自然排序和比較器排序,具體取決于使用的構(gòu)造方法
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
此類實現(xiàn)一個哈希表,該哈希表將鍵映射到相應(yīng)的值。任何非 null 對象都可以用作鍵或值
特點:
- 不允許null鍵和null值
- 線程安全,效率低
HashMap和Hashtable的區(qū)別:
- HashMap是不安全的不同步的效率高的 允許null鍵和null值
- Hashtable是安全的同步的效率低的 不允許null鍵和null值
底層都是哈希表結(jié)構(gòu)
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
以弱鍵 實現(xiàn)的基于哈希表的 Map。在 WeakHashMap 中,當(dāng)某個鍵不再正常使用時,將自動移除其條目。更精確地說,對于一個給定的鍵,其映射的存在并不阻止垃圾回收器對該鍵的丟棄,這就使該鍵成為可終止的,被終止,然后被回收。
丟棄某個鍵時,其條目從映射中有效地移除,因此,該類的行為與其他的 Map 實現(xiàn)有所不同。
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); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot Entity中枚舉類型詳細(xì)使用介紹
本文介紹SpringBoot如何在Entity(DAO)中使用枚舉類型。(本文使用MyBatis-Plus)。在實際開發(fā)中,經(jīng)常會遇到表示類型或者狀態(tài)的情況,比如:有三種支付方式:微信、支付寶、銀聯(lián)。本文介紹如何這種場景的方案對比,并用實例來介紹如何用枚舉這種最優(yōu)雅的來表示2022-10-10詳解基于java的Socket聊天程序——初始設(shè)計(附demo)
本篇文章主要介紹了Socket聊天程序——初始設(shè)計(附demo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12Java圖片處理 (文字水印、圖片水印、縮放、補白)代碼實例
這篇文章主要介紹了Java圖片處理 (文字水印、圖片水印、縮放、補白)代碼實例,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-06-06