LoginSignup
0
0

More than 3 years have passed since last update.

Java Listの入れ子をひとまとめにする

Last updated at Posted at 2020-02-21

Java8以降であれば、flatMapを使えばいけそうです。

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

map()はCollectioinの要素を取り出して変換を行うメソッドで、
flatMap()はCollectionの要素を取り出してStreamへの変換を行うメソッドです。

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0