java Map類

 實現類

類型區別

HashMap

最經常使用的Map,它根據鍵的HashCode 值存儲數據,根據鍵能夠直接獲取它的值,具備很快的訪問速度。HashMap最多隻容許一條記錄的鍵爲Null(多條會覆蓋);容許多條記錄的值爲 Null。非同步的。java

TreeMap

可以把它保存的記錄根據鍵(key)排序,默認是按升序排序,也能夠指定排序的比較器,當用Iterator 遍歷TreeMap時,獲得的記錄是排過序的。TreeMap不容許key的值爲null。非同步的。app

Hashtable

與 HashMap相似,不一樣的是:key和value的值均不容許爲null;它支持線程的同步,即任一時刻只有一個線程能寫Hashtable,所以也致使了Hashtale在寫入時會比較慢。ide

LinkedHashMap

保存了記錄的插入順序,在用Iterator遍歷LinkedHashMap時,先獲得的記錄確定是先插入的.在遍歷的時候會比HashMap慢。key和value均容許爲空,非同步的。函數

四種經常使用Map插入與讀取性能比較

插入10次平均(ms)

  1W 10W 100W
HashMap 56 261 3030
LinkedHashMap 25 229 3069
TreeMap 29 295 4117
Hashtable 24 234 3275

讀取10次平均(ms) 

  1W 10W 100W
HashMap 2 21 220
LinkedHashMap 2 20 216
TreeMap 5 103 1446
Hashtable 2 22 259

加強for循環遍歷

使用keySet()遍歷

for (String key : map.keySet()) { System.out.println(key + " :" + map.get(key)); } 

使用entrySet()遍歷

for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + " :" + entry.getValue()); } 

迭代器遍歷

使用keySet()遍歷

Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) { String key = iterator.next(); System.out.println(key + " :" + map.get(key)); } 

使用entrySet()遍歷 

Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); System.out.println(entry.getKey() + " :" + entry.getValue()); }

性能比較

  1. 加強for循環使用方便,但性能較差,不適合處理超大量級的數據。性能

  2. 迭代器的遍歷速度要比加強for循環快不少,是加強for循環的2倍左右。spa

  3. 使用entrySet遍歷的速度要比keySet快不少,是keySet的1.5倍左右。線程

Map 排序

HashMap、Hashtable、LinkedHashMap排序

//注:TreeMap也可使用此方法進行排序,但不推薦。
Map<String, String> map = new HashMap<String, String>(); map.put("b", "b"); map.put("a", "c"); map.put("c", "a"); // 經過ArrayList構造函數把map.entrySet()轉換成list
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet()); // 經過比較器實現比較排序
Collections.sort(list, new Comparator<Map.Entry<String, String>>() { @Override public int compare(Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) { return mapping1.getKey().compareTo(mapping2.getKey()); } }); for (Map.Entry<String, String> mapping : list) { System.out.println(mapping.getKey() + " :" + mapping.getValue()); }

TreeMap排序

//TreeMap默認按key進行升序排序,若是想改變默認的順序,可使用比較器:
Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() { @Override public int compare(String o1, String o2) { // 降序排序
        return o1.compareTo(o2); } }); map.put("b", "b"); map.put("a", "c"); map.put("c", "a"); for (String key : map.keySet()) { System.out.println(key + " :" + map.get(key)); }

通用排序,接VALUE

Map<String, String> map = new TreeMap<String, String>(); map.put("b", "b"); map.put("a", "c"); map.put("c", "a"); // 經過ArrayList構造函數把map.entrySet()轉換成list
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet()); // 經過比較器實現比較排序
Collections.sort(list, new Comparator<Map.Entry<String, String>>() { @Override public int compare(Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) { return mapping1.getValue().compareTo(mapping2.getValue()); } }); for (String key : map.keySet()) { System.out.println(key + " :" + map.get(key)); }

經常使用API

方法 描述
clear() 從 Map 中刪除全部映射
remove(Object key) 從 Map 中刪除鍵和關聯的值
put(Object key, Object value) 將指定值與指定鍵相關聯
putAll(Map t) 將指定 Map 中的全部映射覆制到此 map
entrySet() 返回 Map 中所包含映射的 Set 視圖。Set 中的每一個元素都是一個 Map.Entry 對象,可使用 getKey() 和 getValue() 方法(還有一個 setValue() 方法)訪問後者的鍵元素和值元素
keySet() 返回 Map 中所包含鍵的 Set 視圖。刪除 Set 中的元素還將刪除 Map 中相應的映射(鍵和值)
values() 返回 map 中所包含值的 Collection 視圖。刪除 Collection 中的元素還將刪除 Map 中相應的映射(鍵和值)
get(Object key) 返回與指定鍵關聯的值
containsKey(Object key) 若是 Map 包含指定鍵的映射,則返回 true
containsValue(Object value) 若是此 Map 將一個或多個鍵映射到指定值,則返回 true
isEmpty() 若是 Map 不包含鍵-值映射,則返回 true
size() 返回 Map 中的鍵-值映射的數目

 

Modifier and Type Method and Description
void clear()
從該地圖中刪除全部的映射(可選操做)。
default V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
嘗試計算指定鍵的映射及其當前映射的值(若是沒有當前映射, null )。
default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
若是指定的鍵還沒有與值相關聯(或映射到 null ),則嘗試使用給定的映射函數計算其值,並將其輸入到此映射中,除非 null
default V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
若是指定的密鑰的值存在且非空,則嘗試計算給定密鑰及其當前映射值的新映射。
boolean containsKey(Object key)
若是此映射包含指定鍵的映射,則返回 true 。
boolean containsValue(Object value)
若是此地圖將一個或多個鍵映射到指定的值,則返回 true 。
Set<Map.Entry<K,V>> entrySet()
返回此地圖中包含的映射的 Set視圖。
boolean equals(Object o)
將指定的對象與此映射進行比較以得到相等性。
default void forEach(BiConsumer<? super K,? super V> action)
對此映射中的每一個條目執行給定的操做,直到全部條目都被處理或操做引起異常。
V get(Object key)
返回到指定鍵所映射的值,或 null若是此映射包含該鍵的映射。
default V getOrDefault(Object key, V defaultValue)
返回到指定鍵所映射的值,或 defaultValue若是此映射包含該鍵的映射。
int hashCode()
返回此地圖的哈希碼值。
boolean isEmpty()
若是此地圖不包含鍵值映射,則返回 true 。
Set<K> keySet()
返回此地圖中包含的鍵的 Set視圖。
default V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
若是指定的鍵還沒有與值相關聯或與null相關聯,則將其與給定的非空值相關聯。
V put(K key, V value)
將指定的值與該映射中的指定鍵相關聯(可選操做)。
void putAll(Map<? extends K,? extends V> m)
將指定地圖的全部映射覆制到此映射(可選操做)。
default V putIfAbsent(K key, V value)
若是指定的鍵還沒有與某個值相關聯(或映射到 null )將其與給定值相關聯並返回 null ,不然返回當前值。
V remove(Object key)
若是存在(從可選的操做),從該地圖中刪除一個鍵的映射。
default boolean remove(Object key, Object value)
僅當指定的密鑰當前映射到指定的值時刪除該條目。
default V replace(K key, V value)
只有當目標映射到某個值時,才能替換指定鍵的條目。
default boolean replace(K key, V oldValue, V newValue)
僅噹噹前映射到指定的值時,才能替換指定鍵的條目。
default void replaceAll(BiFunction<? super K,? super V,? extends V> function)
將每一個條目的值替換爲對該條目調用給定函數的結果,直到全部條目都被處理或該函數拋出異常。
int size()
返回此地圖中鍵值映射的數量。
Collection<V> values()
返回此地圖中包含的值的 Collection視圖。

已知實現類

AbstractMap , Attributes , AuthProvider , ConcurrentHashMap , ConcurrentSkipListMap , EnumMap , HashMap , Hashtable , IdentityHashMap , LinkedHashMap , PrinterStateReasons , Properties , Provider , RenderingHints , SimpleBindings , TabularDataSupport , TreeMap , UIDefaults , WeakHashMap

 

.code

相關文章
相關標籤/搜索