Commons-collections-union
提供:Dev Guides
Apache Commonsコレクション-Union
Apache Commons CollectionsライブラリのCollectionUtilsクラスは、幅広いユースケースをカバーする一般的な操作のためのさまざまなユーティリティメソッドを提供します。 定型コードの記述を避けるのに役立ちます。 同様の機能がJava 8のStream APIで提供されるようになったため、このライブラリはjdk 8より前に非常に便利です。
組合をチェック
CollectionUtilsのunion()メソッドを使用して、2つのコレクションの和集合を取得できます。
宣言
以下はの宣言です
- org.apache.commons.collections4.CollectionUtils.union()*メソッド
public static <O> Collection<O> union(Iterable<? extends O> a,
Iterable<? extends O> b)
パラメーター
- a -最初のコレクションは、nullであってはなりません。
- b -2番目のコレクションは、nullであってはなりません。
戻り値
2つのコレクションの結合。
例
次の例は、org.apache.commons.collections4.CollectionUtils.union()メソッドの使用法を示しています。 2つのリストの結合を取得します。
import java.util.Arrays;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
public class CollectionUtilsTester {
public static void main(String[] args) {
//checking inclusion
List<String> list1 = Arrays.asList("A","A","A","C","B","B");
List<String> list2 = Arrays.asList("A","A","B","B");
System.out.println("List 1: " + list1);
System.out.println("List 2: " + list2);
System.out.println("Union of List 1 and List 2: "
+ CollectionUtils.union(list1, list2));
}
}
出力
次の結果が出力されます。
List 1: [A, A, A, C, B, B]
List 2: [A, A, B, B]
Union of List 1 and List 2: [A, A, A, B, B, C]