类关系
功能介绍
包路径【在哪里】:java.util
功能描述【有什么用】:一个可以映射键-值对的对象。一个Map中不允许存在重复相同的key,而任何一个key仅能映射一个value。
An object that maps keys to values. A map cannot contain duplicate keys;* each key can map to at most one value. _This interface takes the place of the Dictionary class, which* was a totally abstract class rather than an interface.
源码解析
/**
* An object that maps keys to values. A map cannot contain duplicate keys;
* each key can map to at most one value.
*
* @param <K> the type of keys maintained by this map 当前映射维护的key的java类型
* @param <V> the type of mapped values 当前映射维护的value的类型
*
* @author Josh Bloch
* @see HashMap
* @see TreeMap
* @see Hashtable
* @see SortedMap
* @see Collection
* @see Set
* @since 1.2
*/
public interface Map<K,V> {
// Query Operations 查找操作
/**
* Returns the number of key-value mappings in this map. If the
* map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* 返回当前映射集合的映射记录数【即键值对数】,
* 如果当前键值对条数超出Integer.MAX_VALUE,则返回Integer.MAX_VALUE
*
* @return the number of key-value mappings in this map
*/
int size();
/**
* Returns <tt>true</tt> if this map contains no key-value mappings.
*
* 判断当前Map映射是否存在键值对元素,如果存在则返回true
*
* @return <tt>true</tt> if this map contains no key-value mappings
*/
boolean isEmpty();
/**
* Returns <tt>true</tt> if this map contains a mapping for the specified
* key. More formally, returns <tt>true</tt> if and only if
* this map contains a mapping for a key <tt>k</tt> such that
* <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
* at most one such mapping.)
*
* 判断当前Map映射是否存在目标key的映射,如果存在则返回true
*
* @param key key whose presence in this map is to be tested 目标key
* @return <tt>true</tt> if this map contains a mapping for the specified
* key
*
* @throws ClassCastException if the key is of an inappropriate type for
* this map 如果目标key和当前映射的K类型【即key的类型】不一致则抛出ClassCastException异常
*
*/
boolean containsKey(Object key);
/**
* Returns <tt>true</tt> if this map maps one or more keys to the
* specified value. More formally, returns <tt>true</tt> if and only if
* this map contains at least one mapping to a value <tt>v</tt> such that
* <tt>(value==null ? v==null : value.equals(v))</tt>. This operation
* will probably require time linear in the map size for most
* implementations of the <tt>Map</tt> interface.
*
* 判断当前映射是否存在对应的值value的映射,如果存在则返回true
*
* @param value value whose presence in this map is to be tested 目标value
* @return <tt>true</tt> if this map maps one or more keys to the
* specified value
* @throws ClassCastException if the value is of an inappropriate type for
* this map 如果目标value和当前映射的V类型【即value的类型】不一致则抛出ClassCastException异常
*/
boolean containsValue(Object value);
/**
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
*
* 根据传入的key获取当前映射中对应的value,如果存在对应映射关系,则返回对应值,否则返回null
*
* <p>More formally, if this map contains a mapping from a key
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
* key.equals(k))}, then this method returns {@code v}; otherwise
* it returns {@code null}. (There can be at most one such mapping.)
*
* @param key the key whose associated value is to be returned 指定返回value的key
* @return the value to which the specified key is mapped, or
* {@code null} if this map contains no mapping for the key
* @throws ClassCastException if the key is of an inappropriate type for
* this map 如果目标key和当前映射的K类型【即key的类型】不一致则抛出ClassCastException异常
*/
V get(Object key);
// Modification Operations 变更操作
/**
* Associates the specified value with the specified key in this map
* (optional operation). If the map previously contained a mapping for
* the key, the old value is replaced by the specified value. (A map
* <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
* if {@link #containsKey(Object) m.containsKey(k)} would return
* <tt>true</tt>.)
*
* 向当前Map映射中存放键值对,当已存在key对应的映射时,使用传入的value覆盖旧的value,value允许为null
*
* @param key key with which the specified value is to be associated 键
* @param value value to be associated with the specified key 值
*
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>,
* if the implementation supports <tt>null</tt> values.)
*
* 返回值是key先前对应的value,如果没有key对应的映射,那么返回null
*
* @throws UnsupportedOperationException if the <tt>put</tt> operation
* is not supported by this map 如果当前Map映射不允许put操作,抛出异常
*
* @throws ClassCastException if the class of the specified key or value
* prevents it from being stored in this map 如果目标key和当前映射的K类型【即key的类型】不一致则抛出ClassCastException异常
*
* @throws NullPointerException if the specified key or value is null
* and this map does not permit null keys or values 如果目标value和当前映射的V类型【即value的类型】不一致则抛出ClassCastException异常
*
* @throws IllegalArgumentException if some property of the specified key
* or value prevents it from being stored in this map 如果该map阻止保存指定key或value的某些属性,抛出IllegalArgumentException异常
*/
V put(K key, V value);
/**
* Removes the mapping for a key from this map if it is present
* (optional operation). More formally, if this map contains a mapping
* from key <tt>k</tt> to value <tt>v</tt> such that
* <code>(key==null ? k==null : key.equals(k))</code>, that mapping
* is removed. (The map can contain at most one such mapping.)
*
* 从当前Map映射中移除以目标key为键的映射关系,并返回key对应的value,如果没有映射关系则返回null
*
* <p>Returns the value to which this map previously associated the key,
* or <tt>null</tt> if the map contained no mapping for the key.
*
* <p>If this map permits null values, then a return value of
* <tt>null</tt> does not <i>necessarily</i> indicate that the map
* contained no mapping for the key; it's also possible that the map
* explicitly mapped the key to <tt>null</tt>.
*
* <p>The map will not contain a mapping for the specified key once the
* call returns.
*
* @param key key whose mapping is to be removed from the map 目标key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>. 返回目标key对应的value
* @throws UnsupportedOperationException if the <tt>remove</tt> operation
* is not supported by this map 如果当前Map不允许remove操作,则抛出UnsupportedOperationException异常
* @throws ClassCastException if the key is of an inappropriate type for
* this map
*/
V remove(Object key);
// Bulk Operations 扩展操作
/**
* Copies all of the mappings from the specified map to this map
* (optional operation). The effect of this call is equivalent to that
* of calling {@link #put(Object,Object) put(k, v)} on this map once
* for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
* specified map. The behavior of this operation is undefined if the
* specified map is modified while the operation is in progress.
*
* 从指定的map中拷贝所有的映射到该map中。
*
* @param m mappings to be stored in this map
* @throws UnsupportedOperationException if the <tt>putAll</tt> operation
* is not supported by this map
* @throws ClassCastException if the class of a key or value in the
* specified map prevents it from being stored in this map
* @throws NullPointerException if the specified map is null, or if
* this map does not permit null keys or values, and the
* specified map contains null keys or values
* 如果目标Map为null,或者当前Map不允许存在null的key或value,而目标Map中存在,则抛出异常
*
* @throws IllegalArgumentException if some property of a key or value in
* the specified map prevents it from being stored in this map
*/
void putAll(Map<? extends K, ? extends V> m);
/**
* Removes all of the mappings from this map (optional operation).
* The map will be empty after this call returns.
* 清空当前Map所有映射
* @throws UnsupportedOperationException if the <tt>clear</tt> operation
* is not supported by this map
*/
void clear();
// Views 视图
/**
* Returns a {@link Set} view of the keys contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
* the iterator's own <tt>remove</tt> operation), the results of
* the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
* operations.
*
* 返回该map中所有key的Set视图。
* 该set是由map支持的,所以对map的更改将影响到set,反之亦然。
* 如果在迭代set的过程中map被修改了(除了迭代器使用自己的remove操作),那么迭代的结果是undefined。
* 该set支持删除map中映射的操作,其中方法包括Iterator.remove、Set.remove、removeAll、retainAll及clear。
* 但是不支持add或addAll操作。
*
* @return a set view of the keys contained in this map
*/
Set<K> keySet();
/**
* Returns a {@link Collection} view of the values contained in this map.
* The collection is backed by the map, so changes to the map are
* reflected in the collection, and vice-versa. If the map is
* modified while an iteration over the collection is in progress
* (except through the iterator's own <tt>remove</tt> operation),
* the results of the iteration are undefined. The collection
* supports element removal, which removes the corresponding
* mapping from the map, via the <tt>Iterator.remove</tt>,
* <tt>Collection.remove</tt>, <tt>removeAll</tt>,
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
* support the <tt>add</tt> or <tt>addAll</tt> operations.
*
* 返回该map中所有value的Collection视图。
* 该collection是由map支持的,所以对map的更改将影响到collection,反之亦然。
* 如果在迭代collection的过程中map被修改了(除了迭代器使用自己的remove操作),那么迭代的结果是undefined。
* 该collection支持删除map中映射的操作,其中方法包括Iterator.remove、Collection.remove、removeAll、retainAll及clear。
* 但是不支持add或addAll操作。
*
* @return a collection view of the values contained in this map
*/
Collection<V> values();
/**
* Returns a {@link Set} view of the mappings contained in this map.
* The set is backed by the map, so changes to the map are
* reflected in the set, and vice-versa. If the map is modified
* while an iteration over the set is in progress (except through
* the iterator's own <tt>remove</tt> operation, or through the
* <tt>setValue</tt> operation on a map entry returned by the
* iterator) the results of the iteration are undefined. The set
* supports element removal, which removes the corresponding
* mapping from the map, via the <tt>Iterator.remove</tt>,
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
* <tt>clear</tt> operations. It does not support the
* <tt>add</tt> or <tt>addAll</tt> operations.
*
* 返回该map中所有映射的Set视图。
* 该set是由map支持的,所以对map的更改将影响到set,反之亦然。
* 如果在迭代set的过程中map被修改了(除了迭代器使用自己的remove操作,或者setValue操作),那么迭代的结果是undefined
* 该set支持删除map中映射的操作,其中方法包括Iterator.remove、Set.remove、removeAll、retainAll及clear。
* 但是不支持add或addAll操作。
*
* @return a set view of the mappings contained in this map
*/
Set<Map.Entry<K, V>> entrySet();
/**
* A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns
* a collection-view of the map, whose elements are of this class. The
* <i>only</i> way to obtain a reference to a map entry is from the
* iterator of this collection-view. These <tt>Map.Entry</tt> objects are
* valid <i>only</i> for the duration of the iteration; more formally,
* the behavior of a map entry is undefined if the backing map has been
* modified after the entry was returned by the iterator, except through
* the <tt>setValue</tt> operation on the map entry.
*
* 一条map的记录(一个键值对)。
* Map.entrySet方法返回的collection视图中的元素就是这个类(Entry)。
* 唯一一个获得entry引用的方式,是从collection视图的迭代器中获取。
* 这些Map.Entry对象只在迭代期间有效;
* 如果在entry的迭代器返回后,作为支持的map被修改了(除了entry的setValue操作),那么entry的行为是未定义的。
*
* @see Map#entrySet()
* @since 1.2
*/
interface Entry<K,V> {
/**
* Returns the key corresponding to this entry.
*
* 返回该entry对应的key。
*
* @return the key corresponding to this entry
* @throws IllegalStateException implementations may, but are not
* required to, throw this exception if the entry has been
* removed from the backing map. 如果entry已经被支持的map中删除了,抛出IllegalStateException异常
*/
K getKey();
/**
* Returns the value corresponding to this entry. If the mapping
* has been removed from the backing map (by the iterator's
* <tt>remove</tt> operation), the results of this call are undefined.
*
* 返回该entry对应的value。
* 如果在作为支持的map中删除了该映射(用迭代器的remove操作),则返回undefined。
*
* @return the value corresponding to this entry
* @throws IllegalStateException implementations may, but are not
* required to, throw this exception if the entry has been
* removed from the backing map.
*/
V getValue();
/**
* Replaces the value corresponding to this entry with the specified
* value (optional operation). (Writes through to the map.) The
* behavior of this call is undefined if the mapping has already been
* removed from the map (by the iterator's <tt>remove</tt> operation).
*
* 设置该entry对应的value
*
* @param value new value to be stored in this entry
* @return old value corresponding to the entry
* @throws UnsupportedOperationException if the <tt>put</tt> operation
* is not supported by the backing map
* @throws ClassCastException if the class of the specified value
* prevents it from being stored in the backing map
* @throws NullPointerException if the backing map does not permit
* null values, and the specified value is null
* @throws IllegalArgumentException if some property of this value
* prevents it from being stored in the backing map
* @throws IllegalStateException implementations may, but are not
* required to, throw this exception if the entry has been
* removed from the backing map.
*/
V setValue(V value);
/**
* Compares the specified object with this entry for equality.
* Returns <tt>true</tt> if the given object is also a map entry and
* the two entries represent the same mapping. More formally, two
* entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping
* if<pre>
* (e1.getKey()==null ?
* e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
* (e1.getValue()==null ?
* e2.getValue()==null : e1.getValue().equals(e2.getValue()))
* </pre>
* This ensures that the <tt>equals</tt> method works properly across
* different implementations of the <tt>Map.Entry</tt> interface.
*
* 比较两个entry,如果两个entry映射一样【ker和value完全一致】,返回true
*
* @param o object to be compared for equality with this map entry
* @return <tt>true</tt> if the specified object is equal to this map
* entry
*/
boolean equals(Object o);
/**
* Returns the hash code value for this map entry. The hash code
* of a map entry <tt>e</tt> is defined to be: <pre>
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^
* (e.getValue()==null ? 0 : e.getValue().hashCode())
* </pre>
* This ensures that <tt>e1.equals(e2)</tt> implies that
* <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries
* <tt>e1</tt> and <tt>e2</tt>, as required by the general
* contract of <tt>Object.hashCode</tt>.
*
* @return the hash code value for this map entry
* @see Object#hashCode()
* @see Object#equals(Object)
* @see #equals(Object)
*/
int hashCode();
/**
* Returns a comparator that compares {@link Map.Entry} in natural order on key.
*
* <p>The returned comparator is serializable and throws {@link
* NullPointerException} when comparing an entry with a null key.
*
* 返回一个比较器,通过比较entry的key来确定entry之间的大小
*
* @param <K> the {@link Comparable} type of then map keys
* @param <V> the type of the map values
* @return a comparator that compares {@link Map.Entry} in natural order on key.
* @see Comparable
* @since 1.8
*/
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()); //比较key大小,使用的是lambda表达式
}
/**
* Returns a comparator that compares {@link Map.Entry} in natural order on value.
*
* <p>The returned comparator is serializable and throws {@link
* NullPointerException} when comparing an entry with null values.
*
* 返回一个比较器,通过比较entry的value来确定entry之间的大小
*
* @param <K> the type of the map keys
* @param <V> the {@link Comparable} type of the map values
* @return a comparator that compares {@link Map.Entry} in natural order on value.
* @see Comparable
* @since 1.8
*/
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());
}
/**
* Returns a comparator that compares {@link Map.Entry} by key using the given
* {@link Comparator}.
*
* <p>The returned comparator is serializable if the specified comparator
* is also serializable.
*
* 返回一个用于比较entry的比较器,比较规则使用传入的参数对key进行比较
*
* @param <K> the type of the map keys
* @param <V> the type of the map values
* @param cmp the key {@link Comparator}
* @return a comparator that compares {@link Map.Entry} by the key.
* @since 1.8
*/
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());
}
/**
* Returns a comparator that compares {@link Map.Entry} by value using the given
* {@link Comparator}.
*
* <p>The returned comparator is serializable if the specified comparator
* is also serializable.
*
* 返回一个用于比较entry的比较器,比较规则使用传入的参数对value进行比较
*
* @param <K> the type of the map keys
* @param <V> the type of the map values
* @param cmp the value {@link Comparator}
* @return a comparator that compares {@link Map.Entry} by the value.
* @since 1.8
*/
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());
}
}
// Comparison and hashing 比较和哈希
/**
* Compares the specified object with this map for equality. Returns
* <tt>true</tt> if the given object is also a map and the two maps
* represent the same mappings. More formally, two maps <tt>m1</tt> and
* <tt>m2</tt> represent the same mappings if
* <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the
* <tt>equals</tt> method works properly across different implementations
* of the <tt>Map</tt> interface.
* 比较两个Map是否相等
* @param o object to be compared for equality with this map
* @return <tt>true</tt> if the specified object is equal to this map
*/
boolean equals(Object o);
/**
* Returns the hash code value for this map. The hash code of a map is
* defined to be the sum of the hash codes of each entry in the map's
* <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>
* implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
* <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
* {@link Object#hashCode}.
*
* map的equals方法,判断两个map中的键值对集合是不是相同
*
* @return the hash code value for this map
* @see Map.Entry#hashCode()
* @see Object#equals(Object)
* @see #equals(Object)
*/
int hashCode();
// Defaultable methods 默认方法
/**
* Returns the value to which the specified key is mapped, or
* {@code defaultValue} if this map contains no mapping for the key.
*
* map中存在键为key的value就返回该value,不存在则返回一个默认值defaultValue
*
* @since 1.8
*/
default V getOrDefault(Object key, V defaultValue) {
V v;
return (((v = get(key)) != null) || containsKey(key))
? v
: defaultValue;
}
/**
* Performs the given action for each entry in this map until all entries
* have been processed or the action throws an exception. Unless
* otherwise specified by the implementing class, actions are performed in
* the order of entry set iteration (if an iteration order is specified.)
* Exceptions thrown by the action are relayed to the caller.
*
* 效果等同于for(Entry entry:Map.entrySet()),即遍历map中的entryset
*
* @since 1.8
*/
default void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
// 使用lambda表达式
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); //执行某个操作,由参数BiConsumer函数对象决定
}
}
/**
* Replaces each entry's value with the result of invoking the given
* function on that entry until all entries have been processed or the
* function throws an exception. Exceptions thrown by the function are
* relayed to the caller.
*
* 遍历整个map中的entry,将所有key的value值替换为某个新值,新值由function对象的apply方法的返回值决定
*
* @param function the function to apply to each entry
*
* removed during iteration
* @since 1.8
*/
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(function);
// 遍历entrySet
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);
}
}
}
/**
* If the specified key is not already associated with a value (or is mapped
* to {@code null}) associates it with the given value and returns
* {@code null}, else returns the current value.
*
* 向Map中设置键值对,如果当前Map中已经存在当前key对应的映射,则直接返回原映射值,否则设置后返回value
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with the specified key, or
* {@code null} if there was no mapping for the key.
* (A {@code null} return can also indicate that the map
* previously associated {@code null} with the key,
* if the implementation supports null values.)
* @since 1.8
*/
default V putIfAbsent(K key, V value) {
V v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
/**
* Removes the entry for the specified key only if it is currently
* mapped to the specified value.
*
* 移除键值对,只有当Map中对应key的value值与传入的value值相等时,才会移除键值对,否则返回false
*
* @param key key with which the specified value is associated
* @param value value expected to be associated with the specified key
* @return {@code true} if the value was removed
* @since 1.8
*/
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;
}
/**
* Replaces the entry for the specified key only if currently
* mapped to the specified value.
*
* 替换键值,只有当传入的key在Map中存在对应值,且oldValue与原先的值相等时才会替换新的值newValue
*
* @param key key with which the specified value is associated
* @param oldValue value expected to be associated with the specified key
* @param newValue value to be associated with the specified key
* @return {@code true} if the value was replaced
*
* @since 1.8
*/
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;
}
/**
* Replaces the entry for the specified key only if it is
* currently mapped to some value.
*
* 替换键值对,只有原先Map中存在传入的key对应映射关系,才会进行替换
*
* @param key key with which the specified value is associated
* @param value value to be associated with the specified key
* @return the previous value associated with the specified key, or
* {@code null} if there was no mapping for the key.
* (A {@code null} return can also indicate that the map
* previously associated {@code null} with the key,
* if the implementation supports null values.)
* @since 1.8
*/
default V replace(K key, V value) {
V curValue;
if (((curValue = get(key)) != null) || containsKey(key)) {
curValue = put(key, value);
}
return curValue;
}
/**
* If the specified key is not already associated with a value (or is mapped
* to {@code null}), attempts to compute its value using the given mapping
* function and enters it into this map unless {@code null}.
*
* 若key对应的value为null,则将value替换成mappingFunction中apply方法的返回值
*
* @param key key with which the specified value is to be associated
* @param mappingFunction the function to compute a value
* @return the current (existing or computed) value associated with
* the specified key, or null if the computed value is null
*
* @since 1.8
*/
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v;
// 如果当前Map中key对应的value为null或不存在当前key映射
if ((v = get(key)) == null) {
V newValue;
// 执行mappingFunction中apply方法后返回值赋值给newValue,如果newValue不为null,则设置key-newValue映射关系
if ((newValue = mappingFunction.apply(key)) != null) {
put(key, newValue);
return newValue; // 返回newValue
}
}
return v;
}
/**
* If the value for the specified key is present and non-null, attempts to
* compute a new mapping given the key and its current mapped value.
*
* 若当前Map映射中,key对应的value不为null,则将value替换成remappingFunction中apply方法的返回值
*
* @param key key with which the specified value is to be associated
* @param remappingFunction the function to compute a value
* @return the new value associated with the specified key, or null if none
* @since 1.8
*/
default V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
V oldValue;
// 如果当前key对应的值不为null
if ((oldValue = get(key)) != null) {
// 则将执行remappingFunction中apply方法返回值赋值给newValue
V newValue = remappingFunction.apply(key, oldValue);
// 如果newValue不为null
if (newValue != null) {
put(key, newValue); // 使用newValue替代oldValue
return newValue;
} else {
remove(key); // 如果newValue为null,则移除key映射
return null;
}
} else {
return null;
}
}
/**
* Attempts to compute a mapping for the specified key and its current
* mapped value (or {@code null} if there is no current mapping). For
* example, to either create or append a {@code String} msg to a value
* mapping:
*
*
*
* @param key key with which the specified value is to be associated
* @param remappingFunction the function to compute a value
* @return the new value associated with the specified key, or null if none
* @since 1.8
*/
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;
}
}
/**
* If the specified key is not already associated with a value or is
* associated with null, associates it with the given non-null value.
* Otherwise, replaces the associated value with the results of the given
* remapping function, or removes if the result is {@code null}. This
* method may be of use when combining multiple mapped values for a key.
* For example, to either create or append a {@code String msg} to a
* value mapping:
*
*
*
* @param key key with which the resulting value is to be associated
* @param value the non-null value to be merged with the existing value
* associated with the key or, if no existing value or a null value
* is associated with the key, to be associated with the key
* @param remappingFunction the function to recompute a value if present
* @return the new value associated with the specified key, or null if no
* value is associated with the key
* @since 1.8
*/
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);
// 如果当前Map中key对应的值为null,则将传入参数赋值给newValue
// 否则将执行remappingFunction.apply方法返回值赋值给newValue
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
// 如果newValue为null,则移除key映射
if(newValue == null) {
remove(key);
} else {
// 否则设置key-newValue映射
put(key, newValue);
}
return newValue;
}
}
简单用法
public static void main(String[] args) {
Map<Integer,Object> map = new HashMap<Integer, Object>();
map.put(1,256);
map.put(2,"测试");
System.out.println("遍历keySet形式调用");
for(Integer key : map.keySet()){
System.out.println("key="+key+",value="+map.get(key));
}
System.out.println("\n遍历entrySet形式调用");
for(Map.Entry entry : map.entrySet()){
System.out.println("key="+entry.getKey()+",value="+entry.getValue());
}
}
总结
1、Map
2、一个Map中不允许存在重复相同的key,且任何一个key仅能映射一个value。
3、Map内部维护了一个Entry
思考
1、put与putIfAbsent的区别?
**
put方法,无论先前是否存在对应key的映射,也无论先前value是否为null,直接覆盖或设新建传入的key-value键值对
putIfAbsent方法,如果先前已存在对应key的映射,则不进行覆盖并返回先前的value。否则新建key-value键值对
2、是否可以使用Map作为缓存使用?
Map作为**本地缓存,优点是轻量以及快速,生命周期随着 jvm 的销毁而结束。缺点是当存在多实例的时候,每个实例都需要各自保存一份缓存,缓存不具有一致性**。
对标Redis作为分布式缓存来讲,不随JVM销毁而结束,且在多实例的情况下,各实例共用一份缓存,具备数据一致性。缺点就是架构上的复杂性,需保证Redis的可用性。
3、Map是否继承了Collection接口?
Collection是List、Set父接口,不是Map父接口。**