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; } } |