目录
查询 size,isEmpty,containsKey,containsValue
对key和value的操作 getKey,setValue,getValue
返回comparator comparingByKey两个,comparingByValue
单个操作 getOrDefault,putIfAbsent,remove,两个replace
简介
/**
* <p>一个对象,将键映射到值(key to value)。
* 一个map不能包含重复的key,每个key仅仅能映射到最大一个值。
*
* <p>这个接口替代了Dictionary类,那是一个纯粹的抽象类而不是一个接口。
*
* <p>Map接口提供了三个集合视图,允许一个map的内容能被看做一个key的set,
* 一个value的collection,或者key-value映射的set。
* map的顺序为map的视图集合的迭代器返回元素的顺序。
* 一些map的实现,像TreeMap类,对顺序做出特定的保证,一些,像HashMap类,没有保证顺序。
*
* <p>注意:必须小心,如果可变的对象被用作map的key。
* 如果一个对象是map的key,而对象被改变了,影响equals的比较,map的行为没有被指定。
* 一个禁止的特殊情况是不允许map把自己作为一个key。
* 虽然允许map把自己作为一个value,但要告警:
* equals和hashCode方法在这种map上,定义的不好。
*
* <p>所有的通常目的的map实现类应该提供两个标准的构造器,一个无参构造器,创建一个空的map,
* 一个有着单一参数Map类型的构造器,创建一个新的map,有着与它的参数相同的key-value映射。
* 实际上,后一个构造器允许使用者复制任何map,参数一个需要的类的相同的map。
* 没有方法强制这个建议(因为接口不能包含构造器),但是所有的通常目的的JDK中的map实现遵循它。
*
* <p>这个接口中包含破坏性的方法,那是如果map不支持某种修改操作,抛出UnsupportedOperationException。
* 如果是这种情况,如果调用对map无效,这些方法可以,但不必要,抛出UnsupportedOperationException。
* 例如,在一个不可更改的map中调用putAll(Map)方法,可以,但不必要,
* 抛出异常,如果要叠加的map的映射为空。
*
* <p>一些map实现对可以包含的key和value有限制。
* 例如一些实现禁止null的key和value,一些对key的类型有限制。
* 视图插入一个非法的key或value会抛出未检查异常,通常是NullPointerException,ClassCastException。
* 视图查询一个非法的key或value可以抛出一个异常,或者仅仅返回false。
* 一些实现会实现前面的行为,一些会实现后面的行为。
* 更一般地说,尝试对不符合条件的元素执行操作,而该元素的完成不会导致将不符合条件的元素插入到set中,这可能会引发异常,
* 也可能会成功,取决于实现的选择。在这个接口的规范中,这些异常被标记为“可选”。
*
* <p>集合框架接口的许多方法根据equals方法定义。
* 例如containsKey(Object)方法说:返回true,当且仅当map包含一个这样的key,
* <tt>(key==null ? k==null : key.equals(k))</tt>。
* 这个规范不应该解释为调用使用一个非null参数,调用Map.containsKey,会导致任意的键key调用key.equals(k)。
* 实现可以自由的进行优化,从而避免调用equals。
* 例如,一开始比较两个key的hashCode(hashCode的规范保证两个有着不相同的hashcode的对象,一定不相等)。
* 更一般的说,集合框架接口的实现能在它觉得合适的地方,自由的利用Object的底层行为。
*
* <p>一些map的循环遍历操作可能失败,到map直接或间接拥有自己。
* 这包括clone()、equals()、hashCode()和toString()方法。
* 实现可以额外地处理自引用的场景,仅仅大多数目前的实现没有这样做。
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*
* @author Josh Bloch
* @see HashMap
* @see TreeMap
* @see Hashtable
* @see SortedMap
* @see Collection
* @see Set
* @since 1.2
*/
public interface Map<K,V>
查询 size,isEmpty,containsKey,containsValue
// Query Operations
/**
* 返回map中key-value对的数量。如果map包含超过Integer.MAX_VALUE的元素,返回Integer.MAX_VALUE
*
* @return the number of key-value mappings in this map
*/
int size();
/**
* 如果map不包含任何key-value对,返回true
*
* @return <tt>true</tt> if this map contains no key-value mappings
*/
boolean isEmpty();
/**
* 如果map包含有着指定的key的映射,返回true。
* 更正式地,当且仅当map有一个有着这样的key k的映射时,返回true。
* <tt>(key==null ? k==null : key.equals(k))</tt>.
* (最多只能有一个这样的映射)
*
* @param key key whose presence in this map is to be tested
* @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
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified key is null and this map
* does not permit null keys
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
*/
boolean containsKey(Object key);
/**
* 如果map包含有着一个或多个指定的value的映射,返回true。
* 更正式地,当且仅当map有一个或多个有着这样的value v的映射时,返回true。
* <tt>(value==null ? v==null : value.equals(v))</tt>.
* 这个操作可能会需要与map的大小,线性的时间,对于大多数Map接口的实现。
*
* @param value value whose presence in this map is to be tested
* @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
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified value is null and this
* map does not permit null values
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
*/
boolean containsValue(Object value);
对key的增删改查 get,put,remove
/**
* 得到指定key映射的value,或者如果map不包含key对于的映射,返回null
*
* <p>更正式地,如果map包含一个这样的映射,里面的key k 和value v是这样的
* {@code (key==null ? k==null :key.equals(k))} ,那么方法返回v。
* 否则,返回null。(里面最多一个这样的映射)
*
* <p>如果这个map允许null值,那么返回null,不一定表明map不包含这个key对应的映射。
* 也可能key对应的值为null。
* containsKey操作可以用于辨明这两种情况。
*
* @param key the key whose associated value is to be returned
* @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
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified key is null and this map
* does not permit null keys
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
*/
V get(Object key);
// Modification Operations
/**
* 将指定的key与指定的value在这个map中关联(可选操作)。
* 如果map之前包含了这个key的映射,旧的value被指定value替代。
* (一个map m 被认为包含一个key k ,当且仅当m.containsKey(k) 返回true。
*
* @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.)
* @throws UnsupportedOperationException if the <tt>put</tt> operation
* is not supported by this map
* @throws ClassCastException if the class of the specified key or value
* prevents it from being stored in this map
* @throws NullPointerException if the specified key or value is null
* and this map does not permit null keys or values
* @throws IllegalArgumentException if some property of the specified key
* or value prevents it from being stored in this map
*/
V put(K key, V value);
/**
* 将map中key对应的映射移除,如果映射存在(可选操作)。
* 更正式的,如果map包含一个这样的映射,里面的key k 和value v是这样的
* {@code (key==null ? k==null :key.equals(k))} ,那么映射被删除。
* (一个map只能包含最多一个这样的映射)
*
* <p>返回map之前与key关联的value,或者如果map不包含key对应的映射,返回null。
*
* <p>如果这个map允许null值,那么返回null,不一定表明map不包含这个key对应的映射。
* 也可能key对应的值为null。
*
* <p>一旦调用返回,map不会有指定key对应的映射。
*
* @param key key whose mapping is to be removed from the map
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* @throws UnsupportedOperationException if the <tt>remove</tt> operation
* is not supported by this map
* @throws ClassCastException if the key is of an inappropriate type for
* this map
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if the specified key is null and this
* map does not permit null keys
* (<a href="{@docRoot}/java/util/Collection.html#optional-restrictions">optional</a>)
*/
V remove(Object key);
批量操作 putAll,clear
// Bulk Operations
/