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
You can convert it into two list objects one which contains key values and the one which contains map values separately. Create a Map object. Create an ArrayList of integer type to hold the keys of the map. In its constructor call the method keySet() of the Map class.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With