int size();
boolean isEmpty();
boolean containsKey(Object key);
boolean containsValue(Object value);
//将key和value以映射关系放入到map中,如果map中的key已经存在对应的映射,放入的新的value会代替旧的value,并且返回旧的value。
V get(Obeject key);
//将map中对应的key值移除,并且返回对应的value值,如果没有对应的value值或者相应的key值为null则返回null
V remove(Obejct key);
//将一个map中的映射放入到当前map中
void putAll(Map<?extends k,? extends V> m);
//移除当前map中的所有映射关系,在移除之后map为空
void clear();
//将当前map集合中的所有key值放入一个Set集合中,并将其返回
Set<K> keySet();
//将当前map集合中的所有value值放入到Collection集合中,并将其返回
Collection<V> values();
//返回一个Set集合,Set集合存的是一个Entry对象,Entry对象中含有map的key和value
Set<Map.Entry<K,V>> entrySet();
//比较两个map集合的映射关系是否相同
boolean equals(Object o);
//返回这个map集合的哈希值,整个map的哈希值等于每一对Entry对象的哈希值的总和。可以看出如果两个a.equals(b),a.hashCode()==b.hashCode()。
int hashCode();
//如果map集合中不存在key或者不存在key对应的映射,则返回defaultValue即预设值,通常设为0,如果存在这返回v = this.get(key)即map中存在的对应value值。1.8之后
default V getOrDefault(Object key,V defaultValue){
V v;
return (((v = get(key))!=null)||containsKey(key)) ? v:defaultValue;
}
//实现map的遍历更加优雅(lanmda表达式)
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){
throw new ConcurrentModificationException(ise);
}
action.accpet(k,v);
}
}
//java8新特性,lambda
defalut void replaceAll(BiFunction<? super K,? super V,? extend 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);
}
}
}
}
//使用putIfAbsent方法添加键值对,如果map集合中没有该key对应的值,则直接添加,并返回null,如果已经存在对应的值,则依旧为原来的值。
default V putIfAbsent(K ket,V value){
V v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
//只有当前的key对应某个value时,移除该值
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对应某个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;
}
//只有在当前Map中包含key,才用value去替换原来的值,否则什么也不做。
default V replace(K key, V value) {
V curValue;
if (((curValue = get(key)) != null) || containsKey(key)) {
curValue = put(key, value);
}
return curValue;
}
//当Map中不存在key值的映射或映射值为null时,调用mappingFunction,并在mappingFunction执行结果非null时,将结果赋值给key。
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在集合中无法获取到旧的val,或者获取到的旧的val为null,直接返回null。
//根据我们自定义的计算函数和给定的key以及通过给定key获取到的旧的val,计算出一个新的val。
//如果新的val不为null,将key和新的val添加到集合。否则新的val为null,删除这个key并且,直接返回null.
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;
}
}
//
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;
}
}
//value和remappingFunction不能为null
//如果Map中key对应的映射不存在或者为null,则将value关联到key上;否则执行remappingFunction,如果执行结果为null则删除key的映射,否则用该结果跟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;
}
}
Set<Map.Entry<K,V>> entrySet();
方法后的Entry中具有这些方法。
注:需要注意map.keySet();
map.getOrDefault(a,b);
大小写和拼写问题