How to get a List<E> from a HashMap<String,List<E>>
map.values()
returns a Collection<List<E>>
not a List<E>
, if you want the latter then you're required to flatten the nested List<E>
into a single List<E>
as follows:
List<E> result = map.values()
.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
Or use forEach
map.forEach((k,v)->list.addAll(v));
or as Aomine commented use this
map.values().forEach(list::addAll);
Here's an alternate way to do it with Java-9 and above:
List<E> result = map.values()
.stream()
.collect(Collectors.flatMapping(List::stream, Collectors.toList()));