目录
一、背景
最近在看公司的代码时候,发现map使用的频率特别的高,而且还是很高级的样子,于是就整理了一下,map 的源码,一些有用但是不经常用的方法。在此记录分享,所有的代码都是基于jdk1.8的,更高级的可能会有所不同,在此不比较。
二、方法
1.forEach()
遍历map。没有返回值
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的元素进行遍历,没有返回值
// 使用:map.forEach((k,v) -> System.out.println(k+v));
2.replaceAll()
根据key和value,产生一个新的value。
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);
}
}
}
3.putIfAbsent()
英语意思是:添加元素如果不存在。如果存在,返回原来的值
default V putIfAbsent(K key, V value) {
V v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
// 添加元素。如果map里面有该元素,就不添加进去,返回原有的值。
4.computeIfAbsent()
根据方法生成一个值,如果不存在。如果存在,返回原来的值
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;
}
5.computeIfPresent()
根据方法生成一个值,如果存在,返回新的值;如果不存在,返回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;
}
}
6.compute()
根据旧的值计算出新的值。
- 如果旧的值不存在:
- 如果旧的值存在,新的值不存在(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;
}
}
7.merge()
这个是一个添加元素操作,并切自定义解决冲突
- 没有冲突,放进去新的值
- 有冲突,放入方法计算的值
- 为null的会被删除掉
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;
}
三、实现类