Map接口
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供宛城網(wǎng)站建設(shè)、宛城做網(wǎng)站、宛城網(wǎng)站設(shè)計(jì)、宛城網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、宛城企業(yè)網(wǎng)站模板建站服務(wù),10余年宛城做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
Map提供了一種映射關(guān)系,其中的元素是以鍵值對(key-value)的形式存儲(chǔ)的,能夠?qū)崿F(xiàn)根據(jù)key快速查找value;
Map中的鍵值對以Entry類型的對象實(shí)例形式存在;
建(key值)不可重復(fù),value值可以重復(fù),一個(gè)value值可以和很多key值形成對應(yīng)關(guān)系,每個(gè)建最多只能映射到一個(gè)值。
Map支持泛型,形式如:Map<K,V>
Map中使用put(K key,V value)方法添加
Map接口中定義的常用方法
具體使用在實(shí)現(xiàn)類中討論
int size();//獲取Map集合大小(即元素?cái)?shù)量) boolean isEmpty();//判斷是否為空 boolean containsKey(Object key);//判斷是否包含某個(gè)鍵 boolean containsValue(Object value);//判斷是否包含某個(gè)值 V get(Object key);//獲取某個(gè)鍵對應(yīng)的值 V put(K key, V value);//添加鍵值對(K,V) V remove(Object key);//移除某個(gè)鍵對應(yīng)的鍵值對 void putAll(Map<? extends K, ? extends V> m);//添加另一個(gè)Map集合 void clear();//清空所有鍵值對 Set<K> keySet();//獲取鍵的集合 Collection<V> values();//獲取值的集合 Set<Map.Entry<K, V>> entrySet();//獲取鍵值對實(shí)體的集合 interface Entry<K,V>//Map中的內(nèi)部接口
HashMap
基于哈希表的 Map 接口的實(shí)現(xiàn)。此實(shí)現(xiàn)提供所有可選的映射操作,并允許使用 null 值和 null 鍵。(除了非同步和允許使用 null 之外,HashMap 類與 Hashtable 大致相同。)除實(shí)現(xiàn)了Map接口外還實(shí)現(xiàn)了Cloneable,Serializable,繼承了AbstractMap抽象類
此類不保證映射的順序,特別是它不保證該順序恒久不變。
特點(diǎn):
允許鍵為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 接口的哈希表和鏈表實(shí)現(xiàn),具有可預(yù)知的迭代順序
特點(diǎn):
底層數(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 實(shí)現(xiàn)。該映射根據(jù)其鍵的自然順序進(jìn)行排序,或者根據(jù)創(chuàng)建映射時(shí)提供的 Comparator 進(jìn)行排序,
具體取決于使用的構(gòu)造方法。
特點(diǎn):
排序方式類似于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
此類實(shí)現(xiàn)一個(gè)哈希表,該哈希表將鍵映射到相應(yīng)的值。任何非 null 對象都可以用作鍵或值
特點(diǎn):
HashMap和Hashtable的區(qū)別:
底層都是哈希表結(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
以弱鍵 實(shí)現(xiàn)的基于哈希表的 Map。在 WeakHashMap 中,當(dāng)某個(gè)鍵不再正常使用時(shí),將自動(dòng)移除其條目。更精確地說,對于一個(gè)給定的鍵,其映射的存在并不阻止垃圾回收器對該鍵的丟棄,這就使該鍵成為可終止的,被終止,然后被回收。
丟棄某個(gè)鍵時(shí),其條目從映射中有效地移除,因此,該類的行為與其他的 Map 實(shí)現(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, "向上移動(dòng)"); em.put(Direction.DOWN, "向下移動(dòng)"); em.put(Direction.LEFT, "向左移動(dòng)"); em.put(Direction.RIGHT, "向右移動(dòng)"); Set<Direction> keys = em.keySet(); for (Direction key : keys) { String value = em.get(key); System.out.println(key + "|" + value); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:JavaMap接口及其實(shí)現(xiàn)類原理解析
文章路徑:http://www.rwnh.cn/article10/jsdego.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、商城網(wǎng)站、網(wǎng)站維護(hù)、、電子商務(wù)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)