I have a HashMap which I need to filter using some function:
HashMap<Set<Integer>, Double> container
Map.Entry<Set<Integer>, Double> map = container.entrySet()
.stream()
.filter(k -> k.getKey().size() == size)
For the size = 2 the following should be valid:
containerBeforeFilter = {1,2,3} -> 1.5, {1,2} -> 1.3
containerAfterFilter = {1,2} -> 1.3
After I applied the function in the filter, I want to collect results again into a HashMap. However, when I try to apply the method suggested here, I'm getting illegal statements.
So the following statement, applied after the filter, is illegal:
.collect(Collectors.toMap((entry) -> entry.getKey(), (entry) -> entry.getValue()));
What would be the proper way of collecting unchanged map values, where the only criteria is satisfying some key?
UPDATE
The mistake in the above code is the declared type of the variable map
. It should have been Map
rather than Map.Entry
.
So the now functional code is:
Map<Set<Integer>, Double> map = container.entrySet()
.stream()
.filter(k -> k.getKey().size() == size)
.collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue()));