How to extract a List<D> from a HashMap<E,R> using stream

I believe the flatMap step is wrong, since your map step transforms your Stream<R> to a Stream<Set<D>>, so flatMap(List::stream) should be flatMap(Set::stream):

return map.values()
          .stream()
          .filter(x -> x.getSet().stream().anyMatch(t -> t.getCountry().equals(countryname)))
          .map(R::getSet)
          .flatMap(Set::stream)
          .collect(Collectors.toList());

Besides, as you can notice above, the code can be much more readable if you avoid using curly braces when you don't have to.


In order to improve readability of your code I would suggest avoiding unnecessary curly braces in lambdas and even the lambdas themselves. Use method references where possible.

return map.values()
          .stream()
          .map(R::getSet)
          .filter(set -> set.stream()
                            .map(R::getCountry)
                            .anyMatch(countryname::equals))
          .flatMap(Set::stream)
          .collect(toList());

The last flatMap and collect operations can be shortened to one line:

.collect(ArrayList::new, List::addAll, List::addAll); 

Or if you're using Java 9:

.collect(flatMapping(Set::stream, toList()));

However it's just a matter of taste.


get the Elements of the Set whose country names are equal to the given parameter.

You seem to be looking for

// input as parameter to the method for simplicity
public List<D> method(Map<E, R> map, String countryName) {
    return map.values() // Collection<R>
              .stream() // Stream<R>
              .flatMap(a -> a.getSet().stream()) // Stream<D>
              .filter(t -> t.getCountry().equals(countryName)) // filtered
              .collect(Collectors.toList()); // collected to list
}