您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Map的介紹的使用方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Map的介紹的使用方法”吧!
Map是一個(gè)映射接口,不同于List和Set的是他不繼承于Collection接口,Map中存儲(chǔ)的內(nèi)容是鍵值對(duì)(key-value)。
AbstractMap 是繼承于Map的抽象類(lèi),它實(shí)現(xiàn)了Map中的大部分API。其它Map的實(shí)現(xiàn)類(lèi)可以通過(guò)繼承AbstractMap來(lái)減少重復(fù)編碼。
SortedMap 是繼承于Map的接口。SortedMap中的內(nèi)容是排序的鍵值對(duì),排序的方法是通過(guò)比較器(Comparator)。
NavigableMap 是繼承于SortedMap的接口。相比于SortedMap,NavigableMap有一系列的導(dǎo)航方法;如"獲取大于/等于某對(duì)象的鍵值對(duì)"、“獲取小于/等于某對(duì)象的鍵值對(duì)”等等。
Map的主要實(shí)現(xiàn)類(lèi)是HashMap、LinkedHashMap、TreeMap、HashTable等。
繼承于AbstractMap
保存無(wú)序的鍵值對(duì)
非線(xiàn)程安全,元素可為null
繼承于HashMap
保存有序的鍵值對(duì),默認(rèn)提供插入時(shí)的順序,也可構(gòu)造成訪(fǎng)問(wèn)順序。
非線(xiàn)程安全,元素不可為null
繼承于AbstractMap,且實(shí)現(xiàn)了NavigableMap接口
保存有序的鍵值對(duì),默認(rèn)對(duì)key排序,也可構(gòu)造自定義的排序。
非線(xiàn)程安全,元素不可為null
但它繼承于Dictionary,而且也實(shí)現(xiàn)Map接口
保存無(wú)序的鍵值對(duì)
線(xiàn)程安全,元素可為null
interface Entry<K,V> { K getKey(); V getValue(); V setValue(V value); boolean equals(Object o); int hashCode(); public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() { return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getKey().compareTo(c2.getKey()); } public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() { return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> c1.getValue().compareTo(c2.getValue()); } public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) { Objects.requireNonNull(cmp); return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey()); } public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) { Objects.requireNonNull(cmp); return (Comparator<Map.Entry<K, V>> & Serializable) (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue()); } }
/** 返回當(dāng)前Map中鍵值對(duì)的數(shù)量 **/ int size(); /** 返回當(dāng)前Map是否為空 **/ boolean isEmpty(); /** 返回當(dāng)前Map是否包含鍵key **/ boolean containsKey(Object key); /** 返回當(dāng)前Map是否包含值value **/ boolean containsValue(Object value); /** 返回當(dāng)前Map中鍵key對(duì)應(yīng)的值value **/ V get(Object key); /** 將當(dāng)前Map中鍵key對(duì)應(yīng)的值設(shè)置為value **/ V put(K key, V value); /** 移除當(dāng)前Map中鍵key對(duì)應(yīng)的值 **/ V remove(Object key); /** 將m中所有鍵值對(duì)放到當(dāng)前Map中 **/ void putAll(Map<? extends K, ? extends V> m); /** 移除Map中所有鍵值對(duì) **/ void clear(); /** 返回Map中key的集合 **/ Set<K> keySet(); /** 返回Map中value的集合 **/ Collection<V> values(); /** 返回Map中key-value的集合 **/ Set<Map.Entry<K, V>> entrySet();
/** 根據(jù)key獲取value 如果value為空返回默認(rèn)值defaultValue **/ default V getOrDefault(Object key, V defaultValue) { V v; return (((v = get(key)) != null) || containsKey(key)) ? v : defaultValue; } /** 函數(shù)式編程 遍歷map中鍵值對(duì) **/ default void forEach(BiConsumer<? super K, ? super V> action) { Objects.requireNonNull(action); for (Map.Entry<K, V> entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } action.accept(k, v); } } /** 函數(shù)式編程 提供一個(gè)方法 替換所有的value **/ default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { Objects.requireNonNull(function); for (Map.Entry<K, V> entry : entrySet()) { K k; V v; try { k = entry.getKey(); v = entry.getValue(); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } // ise thrown from function is not a cme. v = function.apply(k, v); try { entry.setValue(v); } catch(IllegalStateException ise) { // this usually means the entry is no longer in the map. throw new ConcurrentModificationException(ise); } } } /** 如果map中key為空 則往map中放入key-value **/ default V putIfAbsent(K key, V value) { V v = get(key); if (v == null) { v = put(key, value); } return v; } /** 如果Map中的key對(duì)應(yīng)的值是value 則移除此鍵值對(duì) 否則返回false **/ default boolean remove(Object key, Object value) { Object curValue = get(key); if (!Objects.equals(curValue, value) || (curValue == null && !containsKey(key))) { return false; } remove(key); return true; } /** 如果Map中的key對(duì)應(yīng)的值是oldValue 則用newValue替換oldValue 否則返回false **/ default boolean replace(K key, V oldValue, V newValue) { Object curValue = get(key); if (!Objects.equals(curValue, oldValue) || (curValue == null && !containsKey(key))) { return false; } put(key, newValue); return true; } /** 如果Map中存在key鍵 則替換為value 否則返回false **/ default V replace(K key, V value) { V curValue; if (((curValue = get(key)) != null) || containsKey(key)) { curValue = put(key, value); } return curValue; } /** 如果Map中key對(duì)應(yīng)的value為空 則將key計(jì)算后設(shè)置為key對(duì)應(yīng)的value **/ default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { Objects.requireNonNull(mappingFunction); V v; if ((v = get(key)) == null) { V newValue; if ((newValue = mappingFunction.apply(key)) != null) { put(key, newValue); return newValue; } } return v; } /** 如果Map中 key對(duì)應(yīng)的oldValue為空 則返回null * 否則根據(jù)key和oldValue計(jì)算出新的newValue 如果newValue不為空則put到Map中 如果為空 則移除此鍵值對(duì) **/ default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); V oldValue; if ((oldValue = get(key)) != null) { V newValue = remappingFunction.apply(key, oldValue); if (newValue != null) { put(key, newValue); return newValue; } else { remove(key); return null; } } else { return null; } } /** 根據(jù)Map中的key和對(duì)應(yīng)的oldValue計(jì)算出newValue * 如果newValue為空 且oldValue不為空 則移除此鍵值對(duì) * 如果newValue為空 且oldValue為空且不存在key 返回null * 如果newValue不為空 則put到Map中 **/ default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); V oldValue = get(key); V newValue = remappingFunction.apply(key, oldValue); if (newValue == null) { // delete mapping if (oldValue != null || containsKey(key)) { // something to remove remove(key); return null; } else { // nothing to do. Leave things as they were. return null; } } else { // add or replace old mapping put(key, newValue); return newValue; } } /** 根據(jù)Map中的key查找oldValue 如果oldValue為空 則設(shè)置newValue為oldValue 否則根據(jù)oldValue和value計(jì)算出newValue * 如果newValue為空 則移除此鍵值對(duì) 否則設(shè)置key的值為newValue **/ default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); Objects.requireNonNull(value); V oldValue = get(key); V newValue = (oldValue == null) ? value : remappingFunction.apply(oldValue, value); if(newValue == null) { remove(key); } else { put(key, newValue); } return newValue; }
到此,相信大家對(duì)“Map的介紹的使用方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。