4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Java】複数のList(Collection)を結合する

Last updated at Posted at 2019-07-20

前提条件

List::addAllでも結合そのものはできますが、今回は以下の条件を満たす結合を行います。

  • リストはいくつ入ってもよい
  • 元のリストには影響がない
  • newをしない

やり方

Stream APIflatMapを使えばできます。
配列に置き換える場合.flatMap(Arrays::stream)でできます。

List l1, l2, l3; // 初期化済みのリストを想定

List merged = Stream.of(l1, l2, l3).flatMap(Collection::stream).collect(Collectors.toList());
4
2
0

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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?