I am looking for the first time for Stream API Java 8. And I tried to create a filter to remove elements from the map.
This is my map:
Map<String, Integer> m = new HashMap<>();
I want to delete entries with a value of <= than 0. Therefore, I want to apply a filter and return a new map (Map <String, Integer>).
This is what I tried:
m.entrySet().stream().filter( p -> p.getValue() > 0).collect(Collectors.groupingBy(s -> s.getKey()));
I get HashMap <String, ArrayList <HashMap $ Node β. So, not what I was looking for.
I also tried:
m.entrySet().stream().filter( p -> p.getValue() > 0).collect(Collectors.groupingBy(Map::Entry::getKey, Map::Entry::getValue));
And this causes:
// Error:(50, 132) java: method reference not expected here
Basically, I donβt know how to build the values ββof my new Map.
This is javadoc of Collectors , they wrote some groupingBy examples, but I couldnβt get it working.
So, how do I write to collect so that my Map is built the way I want?
java java-stream
Raul guiu
source share