当前位置: 首页 > news >正文

Map 不常用方法介绍

getOrDefault

尝试获取key对应的值,如果未获取到,就返回默认值。
例子:

private static void testGetOrDefault() {Map<String, String> 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<String, String> map = new HashMap<>(4);map.put("123", "123");// 老写法for (Map.Entry<String, String> 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<K,V>[] tab; Node<K,V> first; int n, i;int binCount = 0;TreeNode<K,V> t = null;Node<K,V> 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<K,V>)first).getTreeNode(hash, key);else {Node<K,V> 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);}elseremoveNode(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<String, Integer> cntMap = new HashMap<>(8);List<String> list = Arrays.asList("apple", "orange", "banana", "orange");// 老写法for (String item : list) {if (cntMap.containsKey(item)) {cntMap.put(item, cntMap.get(item) + 1);} else {cntMap.put(item, 1);}}// 新写法for (String item : list) {cntMap.merge(item, 1, Integer::sum);}
}

可以看到我们使用merge方法的话,只用1行就简洁实现了这个逻辑。

putIfAbsent

不存在key或者值为null时,才将键值对放入Map。跟put方法相比,这个方法不会直接覆盖已有的值,在不允许覆盖旧值的场景使用起来会比较简洁。

private static void testPutIfAbsent() {Map<String, Integer> scoreMap = new HashMap<>(4);scoreMap.put("Jim", 88);scoreMap.put("Lily", 90);// 老写法if (!scoreMap.containsKey("Lily")) {scoreMap.put("Lily", 98);}// 新写法scoreMap.putIfAbsent("Lily", 98);
}

computer

computer方法需要传入2个参数:key、function。主要有3步操作

  • 获取到key对应的oldValue,可能为null
  • 经过function计算获取newValue
  • put(key, newValue)

还是以刚刚统计单次次数需求为例,看一下computer的写法:

private static void testComputer() {Map<String, Integer> cntMap = new HashMap<>(8);List<String> list = Arrays.asList("apple", "orange", "banana", "orange");// 老写法for (String item : list) {if (cntMap.containsKey(item)) {cntMap.put(item, cntMap.get(item) + 1);} else {cntMap.put(item, 1);}}// 新写法for (String item : list) {cntMap.compute(item, (k, v) -> {if (v == null) {v = 1;} else {v += 1;}return v;});}
}

computeIfAbsent

看名字就知道是compute方法衍生出来的方法,这个方法只在key不存在的时候,执行computer计算,如果说key对应的value存在,就直接返回这个value。

例如,我们需要计算斐波那锲数列的时候,可以使用这个方法来简化代码:

private static void testComputerIfAbsent() {Map<Integer, Integer> fabMap = new ConcurrentHashMap<>(16);fabMap.put(0, 1);fabMap.put(1, 1);System.out.println(fab(5, fabMap));
}private static Integer fab(Integer index, Map<Integer, Integer> fabMap) {return fabMap.computeIfAbsent(index, i -> fab(i - 2, fabMap) + fab(i - 1, fabMap));
}

computeIfPresent

这个是computeIfAbsent的姊妹方法,区别在于,这个方法是只有key存在的时候,才去执行computer计算和值的更新。

replace

这个方法的效果是:

  • 如果key存在,则更新值
  • 如果key不存在,什么也不做
http://www.lryc.cn/news/475748.html

相关文章:

  • 论文翻译:ICLR 2024.DETECTING PRETRAINING DATA FROM LARGE LANGUAGE MODELS
  • Spring 框架精髓:从基础到分布式架构的进阶之路
  • 深入理解C++ Lambda表达式:语法、用法与原理及其包装器的使用
  • C# 编程语言:跨时代的革命
  • 恋爱脑学Rust之Box与RC的对比
  • Rust 力扣 - 1423. 可获得的最大点数
  • Android15音频进阶之Cuttlefish搭建音频开发环境(九十二)
  • 发现不为人知的AI宝藏:发现AI新天地! —— 《第八期》
  • 基于物联网设计的地下煤矿安全监测与预警
  • Java 23 的12 个新特性!!
  • .NET 8 中 Entity Framework Core 的使用
  • ai数字人分身123口播克隆数字人小程序源码_博纳软云
  • 从0开始学PHP面向对象内容之(类,对象,构造/析构函数)
  • openGauss数据库-头歌实验1-5 修改数据库
  • 《JVM第3课》运行时数据区
  • 阅读笔记 Contemporary strategy analysis Chapter 14
  • 2024网鼎杯青龙组wp:Crypto2
  • 能通过Ping命令访问CentOS 9 Stream,但在使用Xshell连接
  • Oracle 第19章:高级查询技术
  • Excel:vba运行时错误“7“:内存溢出错误
  • 【MyBatis源码】BoundSql分析
  • KTHREAD--InitialStack和KernelStack和TSS的esp0
  • Skia基础运用(Ubuntu环境下使用BUILD.gn)
  • Vue中props和data的优先级哪个更高?
  • springboot2.x使用SSE方式代理或者转发其他流式接口
  • consul入门教程
  • 软考:大数据架构设计
  • token无感刷新+处理并发的后端方案
  • 【系统设计】让 Java “动起来”:动态语言与静态语言的比较及 DSL 实现
  • TCP Analysis Flags 之 TCP Keep-Alive