且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java8映射如何简单地将一些元素添加到列表值

更新时间:2023-11-27 09:59:28

使用computeIfAbsent:

map.computeIfAbsent(work, k -> new ArrayList<>()).add("aaa");

如果列表中不存在新列表,则会将其存储在地图中,并返回新列表或现有列表.

Which stores a new list in the map if it does not already exist and returns the new or existing list.