Java – filter concurrenthashmap by value
•
Java
I'm trying to filter the size of concurrenthashmap < string, LinkedList < string > > through LinkedList < string >
In other words, I want to filter out the elements in concurrenthashmap, where the size of LinkedList < string > is Greater than 4 How do I do it with Java 8?
Solution
If you have a ConcurrentMap, you can simply create an entry flow, by calling entrySet (), then calling stream (), and by maintaining the value of the filter value by more than 4 entries. Finally, you can collect it again into the built-in collectors in concurrentmap 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));
Alternatively, you can do this in place by modifying the map
map.values().removeIf(l -> l.size() <= 4);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码