Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 221
Biscuit128 Avatar asked Jun 23 '15 09:06

Biscuit128


People also ask

How to Convert List of map to List of objects in Java?

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.


1 Answers

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());
like image 63
Eran Avatar answered Sep 23 '22 02:09

Eran