package java.util;publicinterfaceMap<K,V>{// 返回map中的键值对个数intsize();// 判断map中是否包含键值对booleanisEmpty();// 判断map是否存在指定的keybooleancontainsKey(Object key);// 判断map是否存在指定的valuebooleancontainsValue(Object value);// 在map中通过key获取对应的value
V get(Object key);// 向map存放键值对
V put(K key, V value);// 在map中通过key移除指定的键值对
V remove(Object key);// 向map集合中添加一个map集合voidputAll(Map<?extendsK,?extendsV> m);// 从map中删除所有的键值对voidclear();// 从map中获取获取所有的key
Set<K>keySet();// map集合中的所有键值对象,这些键值对象将存放在另一个集合对象中
Collection<V>values();// 获取map所有的键值对放入Set集合中
Set<Map.Entry<K, V>>entrySet();// 内部接口EntryinterfaceEntry<K,V>{// 获取key
K getKey();// 获取value
V getValue();// 设置value
V setValue(V value);// 比较booleanequals(Object o);// hashCodeinthashCode();publicstatic<K extendsComparable<?super K>, V> Comparator<Map.Entry<K,V>>comparingByKey(){return(Comparator<Map.Entry<K, V>>& Serializable)(c1, c2)-> c1.getKey().compareTo(c2.getKey());}publicstatic<K, V extendsComparable<?super V>> Comparator<Map.Entry<K,V>>comparingByValue(){return(Comparator<Map.Entry<K, V>>& Serializable)(c1, c2)-> c1.getValue().compareTo(c2.getValue());}publicstatic<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());}publicstatic<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());}}booleanequals(Object o);inthashCode();// 获取默认default V getOrDefault(Object key, V defaultValue){
V v;// 根据key获取value判断是否为null,如果不为空则返回value否则判断key是否在map中存在// 如果存在则返回对应的value否则返回defaultValue传入的默认valuereturn(((v =get(key))!= null)||containsKey(key))? v
: defaultValue;}// 遍历defaultvoidforEach(BiConsumer<?super K,?super V> action){// 判断当前对象是否为空
Objects.requireNonNull(action);// 编辑mapfor(Map.Entry<K, V> entry :entrySet()){
K k;
V v;try{// 获取key
k = entry.getKey();// 获取value
v = entry.getValue();}catch(IllegalStateException ise){// this usually means the entry is no longer in the map.thrownewConcurrentModificationException(ise);}// 将key和value传给BiConsumer接口的中的accept方法
action.accept(k, v);}}defaultvoidreplaceAll(BiFunction<?super K,?super V,?extendsV> 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.thrownewConcurrentModificationException(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.thrownewConcurrentModificationException(ise);}}}// 如果集合中不存在value时就放入集合中default V putIfAbsent(K key, V value){// 根据key获取对应的value
V v =get(key);// 判断value是否为空,为空时进入方法if(v == null){// 将key 和 value 放入map中
v =put(key, value);}// 返回value值return v;}// 移除defaultbooleanremove(Object key, Object value){// 通过key获取对应的value
Object curValue =get(key);// 判断将要移除的value和通过key获取的value是否相等,相等则不进入方法if(!Objects.equals(curValue, value)||// 如果通过key获取的value值为空并且map中不存在key(curValue == null &&!containsKey(key))){// 返回false 移除失败returnfalse;}// 不相等通过key移除键值对remove(key);// 返回true 移除成功returntrue;}// 替换defaultbooleanreplace(K key, V oldValue, V newValue){// 通过key获取以前的value
Object curValue =get(key);// 判断curValue 和oldValue 是否相等,相等则不进入方法if(!Objects.equals(curValue, oldValue)||// 判断curValue为空并且map中不存在key(curValue == null &&!containsKey(key))){// 返回false 替换失败returnfalse;}// 否则成功put(key, newValue);returntrue;}// 替换default V replace(K key, V value){
V curValue;// 判断通过key获取的value不为空或者map中存在keyif(((curValue =get(key))!= null)||containsKey(key)){// 替换新的value
curValue =put(key, value);}// 返回之前旧的valuereturn curValue;}default V computeIfAbsent(K key,
Function<?super K,?extendsV> 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;}default V computeIfPresent(K key,
BiFunction<?super K,?super V,?extendsV> 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;}}default V compute(K key,
BiFunction<?super K,?super V,?extendsV> remappingFunction){
Objects.requireNonNull(remappingFunction);
V oldValue =get(key);
V newValue = remappingFunction.apply(key, oldValue);if(newValue == null){// delete mappingif(oldValue != null ||containsKey(key)){// something to removeremove(key);return null;}else{// nothing to do. Leave things as they were.return null;}}else{// add or replace old mappingput(key, newValue);return newValue;}}default V merge(K key, V value,
BiFunction<?super V,?super V,?extendsV> 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;}}