Java 8 - return List (keyset) opposed to List<Map.Entry<Integer, CheckBox>>

805 Views Asked by At

I am trying to use java 8 to return me a list of key values(Integers) in which the value (Checkbox) is checked. The map I am trying to process is of the following form.

Map<Integer, CheckBox> 

The aim is to return the key set for all values where the check box value is checked.

If I do the following

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

then I get back a List<Map.Entry<Integer, CheckBox>> Is there anyway to do this all in one line without processing the Map.Entry values so I can just get a list of integers?

Thanks

1

There are 1 best solutions below

1
On BEST ANSWER

You can add a map call to extract the key from the Entry :

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