按值过滤 ConcurrentHashMap
Filter ConcurrentHashMap by value
我正在尝试按 LinkedList<String>
的大小过滤 ConcurrentHashMap<String, LinkedList<String>>
。
换句话说,我想过滤掉ConcurrentHashMap
中LinkedList<String>
大小大于4的元素,如何通过Java8来完成?
如果您有一个 ConcurrentMap
,您可以通过调用 entrySet()
and then stream()
and keep the entries where the value has a length greater than 4 by applying a filter
. Finally, you can collect that again into a ConcurrentMap
with the built-in Collectors.toConcurrentMap
.
简单地创建其条目流
ConcurrentMap<String, LinkedList<String>> map = new ConcurrentHashMap<>();
ConcurrentMap<String, LinkedList<String>> result =
map.entrySet()
.stream()
.filter(e -> e.getValue().size() > 4)
.collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue));
或者,您可以通过使用
修改地图来就地完成
map.values().removeIf(l -> l.size() <= 4);
我正在尝试按 LinkedList<String>
的大小过滤 ConcurrentHashMap<String, LinkedList<String>>
。
换句话说,我想过滤掉ConcurrentHashMap
中LinkedList<String>
大小大于4的元素,如何通过Java8来完成?
如果您有一个 ConcurrentMap
,您可以通过调用 entrySet()
and then stream()
and keep the entries where the value has a length greater than 4 by applying a filter
. Finally, you can collect that again into a ConcurrentMap
with the built-in Collectors.toConcurrentMap
.
ConcurrentMap<String, LinkedList<String>> map = new ConcurrentHashMap<>();
ConcurrentMap<String, LinkedList<String>> result =
map.entrySet()
.stream()
.filter(e -> e.getValue().size() > 4)
.collect(Collectors.toConcurrentMap(Map.Entry::getKey, Map.Entry::getValue));
或者,您可以通过使用
修改地图来就地完成map.values().removeIf(l -> l.size() <= 4);