Map是我们日常编程中十分常用的数据接口,的在JDK8中,Map引入了几个新方法,可以简化我们对Map中数据的操作。
目前JDK的最新LTS版本已经更新到21了,这几个在JDK8引入的Map”新“方法其实也是”老“方法了,还没熟练使用也太out了,快来看看你都”学废“了吗?
getOrDefault
这个方法名很直观,见名知意:尝试获取key对应的值,如果未获取到,就返回默认值。
看一个使用的例子,新写法会比老写法更加简洁:
private static void testGetOrDefault() {
Map map = new HashMap<>(4);
map.put("123", "123");
String key = "key";
String defaultValue = "defaultValue";
// 老写法
String oldValue = defaultValue;
if (map.containsKey(key)) {
oldValue = map.get(key);
}
System.out.println("oldValue = " + oldValue);
// 新写法
String newValue = map.getOrDefault(key, defaultValue);
System.out.println("newValue = " + newValue);
}
foreach
看方法名也可以知道,这个方法是遍历map的数据使用的。
如果没有foreach,我们遍历map的时候一般是使用增强for循环,有了这个方法后,可以更加方便使用entry中的key和val:
private static void testForeach() {
Map map = new HashMap<>(4);
map.put("123", "123");
// 老写法
for (Map.Entry entry : map.entrySet()) {
System.out.printf("老写法 key = %s, value = %s%n", entry.getKey(), entry.getValue());
}
// 新写法
map.forEach((key, value) -> System.out.printf("新写法 key = %s, value = %s%n", key, value));
}
merge
从名字可以想到,是合并entry使用的,但是具体是怎么合并呢?
看一下日常最常用的Map实现类HashMap对merge方法的实现
@Override
public V merge(K key, V value,
BiFunction super V, ? super V, ? extends V> remappingFunction) {
if (value == null || remappingFunction == null)
throw new NullPointerException();
int hash = hash(key);
Node[] tab; Node first; int n, i;
int binCount = 0;
TreeNode t = null;
Node old = null;
if (size > threshold || (tab = table) == null ||
(n = tab.length) == 0)
n = (tab = resize()).length;
if ((first = tab[i = (n - 1) & hash]) != null) {
if (first instanceof TreeNode)
old = (t = (TreeNode)first).getTreeNode(hash, key);
else {
Node e = first; K k;
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k)))) {
old = e;
break;
}
++binCount;
} while ((e = e.next) != null);
}
}
if (old != null) {
V v;
if (old.value != null) {
int mc = modCount;
v = remappingFunction.apply(old.value, value);
if (mc != modCount) {
throw new ConcurrentModificationException();
}
} else {
v = value;
}
if (v != null) {
old.value = v;
afterNodeAccess(old);
}
else
removeNode(hash, key, null, false, true);
return v;
} else {
if (t != null)
t.putTreeVal(this, tab, hash, key, value);
else {
tab[i] = newNode(hash, key, value, first);
if (binCount >= TREEIFY_THRESHOLD - 1)
treeifyBin(tab, hash);
}
++modCount;
++size;
afterNodeInsertion(true);
return value;
}
}
代码比较长,但是实现的效果比较容易描述:这个方法接收3个参数:key、value、function。
-
如果key存在,将value按照function做1次计算后,更新到Map中
-
如果key不存在,将key-value放入Map中
这个方法在某些场景中挺好用的,代码简洁易懂,例如:我们有1个List,要统计List中每个元素出现的次数。我们要实现的逻辑是,遍历List中的每个元素,如果这个元素在Map中存在,Map中的值+1;如果不存在,则放入Map中,次数(值)为1。
private static void testMerge() {
Map cntMap = new HashMap<>(8);
List list = Arrays.asList("apple", "orange", "banana", "orange"