Nesting Java Lists together

If it is Java8 or later, it seems that you should use flatMap.

Map<UserId, List<User>> userMap;
List<User> users = userMap.values()
  .stream()
  .flatMap(Collection::stream) // List<User> -> Stream<User>Conversion to
  .collect(Collectors.toList());

map () is a method that extracts the elements of Collectioin and performs conversion. flatMap () is a method that takes out the elements of Collection and converts them to Stream.

Recommended Posts

Nesting Java Lists together
Combine Java8 lists
Study java arrays, lists, maps
[Java] Combine multiple Lists (Collections)
Java
Java
Catch multiple exceptions together in java