Map接口規範了Map數據結構的通用API(也含有幾個用於簡化操作的default方法,default是JDK8的新特性,它是接口中聲明的方法的默認實現,即非抽象方法)並且還在內部定義了Entry接口(鍵值對的實體類),在JDK中提供的所有Map數據結構都實現了Map接口,下面爲Map接口的源碼(代碼中的註釋太長了,基本都是些實現的規範,爲了篇幅我就儘量省略了)。
package java.util;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.io.Serializable;
public interface Map<K,V> {
// 查詢操作
/**
* 返回這個Map中所包含的鍵值對的數量,如果大於Integer.MAX_VALUE,
* 則應該返回Integer.MAX_VALUE。
*/
int size();
/**
* Map是否爲空。
*/
boolean isEmpty();
/**
* Map中是否包含key,如果是返回true,否則false。
*/
boolean containsKey(Object key);
/**
* Map中是否包含value,如果是返回true,否則false。
*/
boolean containsValue(Object value);
/**
* 根據key查找value,如果Map不包含該key,則返回null。
*/
V get(Object key);
// 修改操作
/**
* 添加一對鍵值對,如果Map中已含有這個key,那麼新value將覆蓋掉舊value,
* 並返回舊value,如果Map中之前沒有這個key,那麼返回null。
*/
V put(K key, V value);
/**
* 刪除指定key並返回之前的value,如果Map中沒有該key,則返回null。
*/
V remove(Object key);
// 批量操作
/**
* 將指定Map中的所有鍵值對批量添加到當前Map。
*/
void putAll(Map<? extends K, ? extends V> m);
/**
* 刪除Map中所有的鍵值對。
*/
void clear();
// 集合視圖
/**
* 返回包含Map中所有key的Set,對該視圖的所有修改操作會對Map產生同樣的影響,反之亦然。
*/
Set<K> keySet();
/**
* 返回包含Map中所有value的集合,對該視圖的所有修改操作會對Map產生同樣的影響,反之亦然。
*/
Collection<V> values();
/**
* 返回包含Map中所有鍵值對的Set,對該視圖的所有修改操作會對Map產生同樣的影響,反之亦然。
*/
Set<Map.Entry<K, V>> entrySet();
/**
* Entry代表一對鍵值對,規範了一些基本函數以及幾個已實現的類函數(各種比較器)。
*/
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());
}
}
// 比較和hashing
/**
* 將指定的對象與此Map進行比較是否相等。
*/
boolean equals(Object o);
/**
* 返回此Map的hash code。
*/
int hashCode();
// 默認方法(非抽象方法)
/**
* 根據key查找value,如果該key不存在或等於null則返回defaultValue。
*/
default V getOrDefault(Object key, V defaultValue) {
V v;
return (((v = get(key)) != null) || containsKey(key)) ? v : defaultValue;
}
/**
* 遍歷Map並對每個鍵值對執行指定的操作(action)。
* BiConsumer是一個函數接口(具有一個抽象方法的接口,用於支持Lambda),
* 它代表了一個接受兩個輸入參數的操作,且不返回任何結果。
* 至於它奇怪的名字,根據Java中的其他函數接口的命名規範,Bi應該是Binary的縮寫,意思是二元的。
*/
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);
}
}
/**
* 遍歷Map,然後調用傳入的函數function生成新value對舊value進行替換。
* BiFunction同樣是一個函數接口,它接受兩個輸入參數並且返回一個結果。
*/
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);
}
}
}
/**
* 如果指定的key不存在或者關聯的value爲null,則添加鍵值對。
*/
default V putIfAbsent(K key, V value) {
V v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
/**
* 當指定key關聯的value與傳入的參數value相等時刪除該key。
*/
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;
}
/**
* 當指定key關聯的value與oldValue相等時,使用newValue進行替換。
*/
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;
}
/**
* 當指定key關聯到某個value時進行替換。
*/
default V replace(K key, V value) {
V curValue;
if (((curValue = get(key)) != null) || containsKey(key)) {
curValue = put(key, value);
}
return curValue;
}
/**
* 當指定key沒有關聯到一個value或者value爲null時,調用mappingFunction生成值並添加鍵值對到Map。
* Function是一個函數接口,它接受一個輸入參數並返回一個結果,如果mappingFunction返回的結果
* 也爲null,那麼將不會調用put。
*/
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;
}
/**
* 當指定key關聯到一個value並且不爲null時,調用remappingFunction生成newValue,
* 如果newValue不爲null,那麼進行替換,否則刪除該key。
*/
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;
}
}
/**
* remappingFunction根據key與其相關聯的value生成newValue,
* 當newValue等於null時刪除該key,否則添加或者替換舊的映射。
*/
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;
}
}
/**
* 當指定key沒有關聯到一個value或者value爲null,將它與傳入的參數value
* 進行關聯。否則,調用remappingFunction生成newValue並進行替換。
* 如果,newValue等於null,那麼刪除該key。
*/
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;
}
}
需要注意一點,這些default方法都是非線程安全的,任何保證線程安全的擴展類都必須重寫這些方法,例如ConcurrentHashMap。
下圖爲Map的繼承關係結構圖,它也是本文接下來將要分析的Map實現類的大綱,這些實現類都是比較常用的,在JDK中Map的實現類有幾十個,大部分都是我們用不到的,限於篇幅原因就不一一講解了(本文包含許多源碼與對實現細節的分析,建議讀者抽出一段連續的空閒時間靜下心來慢慢閱讀)。