Java 8 - return list (key set) opposite to <Map.Entry <Integer, CheckBox >>

I am trying to use java 8 to return me a list of key values ​​(Integers) in which the value is marked (Checkbox). The card I'm trying to process has the following form.

Map<Integer, CheckBox> 

The goal is to return a set of keys for all values ​​where the checkbox is checked.

If I do the following

 checkBoxes.entrySet().stream().filter(c -> c.getValue().getValue()) .collect(Collectors.toList()); 

then I will return to List<Map.Entry<Integer, CheckBox>> . Anyway, to do this all on one line without processing Map.Entry values ​​so that I can just get a list of integers?

thanks

+6
source share
1 answer

You can add a map call to retrieve the key from the entry:

 List<Integer> keys = checkBoxes.entrySet().stream() .filter(c -> c.getValue().getValue()) .map(Map.Entry::getKey) .collect(Collectors.toList()); 
+8
source

Source: https://habr.com/ru/post/989600/


All Articles