包
package java.util;
简介
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
结构
查询操作
// Query Operations
int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
V get(Object key);
修改操作
// Modification Operations
V put(K key, V value);
V remove(Object key);
批量操作
// Bulk Operations
void putAll(Map<? extends K, ? extends V> m);
void clear();
查看方法
// Views
Set<K> keySet();
Collection<V> values();
Set<Map.Entry<K, V>> entrySet();
内部类 Entry
K getKey();
V getValue();
V setValue(V value);
比较和哈希
参考文章:
JAVA泛型知识(二)--> <? extends T>和<? super T>
JAVA8的java.util.function包
// Comparison and hashing
boolean equals(Object o);
int hashCode();
boolean equals(Object o);
int hashCode();
public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey();
public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue();
public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp);
public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp);
默认方法
参考文章:
java接口中的default方法
// Defaultable methods
default V getOrDefault(Object key, V defaultValue);
default void forEach(BiConsumer<? super K, ? super V> action);
default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function);
default V putIfAbsent(K key, V value);
default boolean remove(Object key, Object value);
default boolean replace(K key, V oldValue, V newValue);
default V replace(K key, V value)
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction);
default V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction);
default V computeIfPresent(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction);
default V compute(K key,
BiFunction<? super K, ? super V, ? extends V> remappingFunction);
default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction)