I have a map which has as key string, and as a value a list of objects Names
. In this map, I saved information some years about a Names
. Each object of the `` class has a name, gender and rank atributes.
Map <String, ArrayList<Names>> myMap = new TreeMap<>();
I tried to do this but it doesn't work:
List<Names> filterList = myMap.filter((k, v) -> v.name == name).collect(Collectors.toList);
Assuming that the input map is like this:
Map<String, List<BabyName>> inputMap;
The list of BabyName
should be retrieved using flatMap
and filter
operations of Stream API:
List<BabyName> result = inputMap.values() // Collection<List<BabyName>>
.stream() // Stream<List<BabyName>>
.flatMap(List::stream) // Stream<BabyName>
.filter(bn -> bn.getRank() == 3) // filtered Stream<BabyName>
.collect(Collectors.toList());