CollectionUtils の概要

多くの便利なメソッドを含む別の汎用ユーティリティ クラス:

addAll(Collection<C> コレクション, C...要素) 配列 C のすべての要素をコレクションに追加します
addIgnoreNull(Collection<T> コレクション, T オブジェクト) null でない場合は要素を追加します
containsAll(コレクション<?> coll1, コレクション<?> coll2) collection1 に collection2 のすべての要素が含まれていることを確認します。
containsAny(コレクション<?> coll1, コレクション<?> coll2) collection1 に collection2 の要素が少なくとも 1 つ含まれていることを確認します。
countMatches(Iterable<C> input, Predicate<? super C> predicate) コレクション内の述語ルールを満たす要素の数をカウントします。
disjunction(Iterable<? extends O> a, Iterable<? extends O> b) 両方のコレクションではなく、1 つのコレクションのみに含まれるオブジェクトを返します。
Intersection(Iterable<? extends O> a, Iterable<? extends O> b) 両方のコレクションの一部であるオブジェクトのコレクションを一度に返します。
Union(Iterable<? extends O> a, Iterable<? extends O> b) 少なくとも 1 つのコレクションの一部であるオブジェクトのコレクションを返します。
空のコレクション() 特別な空のコレクションを返します
emptyIfNull(Collection<T> コレクション) collection == null の場合、空のコレクションを返します。
存在します(Iterable<C> 入力、Predicate<? super C> 述語) コレクション内に述語ルールを満たす要素があるかどうかを確認します
extractSingleton(Collection<E> コレクション) コレクションの唯一の要素を返します
filter(Iterable<T> コレクション、Predicate<? super T> 述語) コレクションの要素をフィルタリングします
find(Iterable<T> コレクション、Predicate<? super T> 述語) コレクションの要素を探しています
isEmpty(コレクション<?> coll) コレクションが空かどうかを確認します
isEqualCollection(コレクション<?> a, コレクション<?> b) コレクションを比較します
isFull(Collection<? extends Object> coll) 要素をコレクションに追加できるかどうかを確認します
isNotEmpty(コレクション<?> coll) コレクションが空でないかどうかを確認する
isSubCollection(コレクション<?> a, コレクション<?> b) コレクション B がコレクション A 内にあるかどうかを確認します
matchsAll(Iterable<C> input, Predicate<? super C> predicate) コレクションのすべての要素が述語ルールを満たしていることを確認します
RemoveAll(Collection<E> コレクション, Collection<?> 削除) コレクションのすべての要素を削除します
keepAll(Collection<C> コレクション, Collection<?> 保持) 保持にも含まれるコレクションのすべての要素を含むコレクションを返します。
reverseArray(オブジェクト[]配列) 配列を逆方向に反転します
unmodifiableCollection(Collection<? extends C> コレクション) コレクションのラッパーを返します。コレクションを変更しようとすると例外がスローされます。

いくつかの方法を見てみましょう。

コレクションが空かどうかを確認する方法

コレクションが空かどうかを簡単に判断できるのは次のとおりです。

CollectionUtils.isEmpty(null); // true
CollectionUtils.isEmpty(new LinkedList<>()); // true
CollectionUtils.isEmpty(Collections.singleton(new int[]{2, 1})); // false

コレクションが空でないことを確認するのも同様に簡単です。

CollectionUtils.isNotEmpty(null); // false
CollectionUtils.isNotEmpty(new LinkedList<>()); // false
CollectionUtils.isNotEmpty(Collections.singleton(new int[]{2, 1})); // true

例:

User ivan = new User("Ivan", "ivan@email.com");
User petr = new User("Petr", "petr@email.com");

List<User> users = new ArrayList<>();

System.out.println(CollectionUtils.isEmpty(users)); // true
System.out.println(CollectionUtils.isNotEmpty(users)); // false

users.add(ivan);
users.add(petr);

System.out.println(CollectionUtils.isEmpty(users)); // false
System.out.println(CollectionUtils.isNotEmpty(users)); // true

users = null;
System.out.println(CollectionUtils.isEmpty(users)); // true
System.out.println(CollectionUtils.isNotEmpty(users)); // false

Apache Commons Collections のガイド

セットに対する操作

集合論を扱う必要がある場合は、集合に対して 4 つの基本演算 (和集合、積集合、差分、選言) があることをご存知でしょう。

これらの操作のために、 CollectionUtilsクラスには4 つのメソッドがあります。

  • 連合()
  • 交差点()
  • 論理和()
  • 減算()

以下に簡単な例をいくつか示します。

協会

List<String> firstList = Arrays.asList("1", "2", "3", "4", "5", "6");
List<String> secondList = Arrays.asList("2", "3", "6", "7", "8", "9");

//combine two sets
Collection<String> result = CollectionUtils.union(firstList, secondList);
System.out.println(ArrayUtils.toString(result));     //[1, 2, 3, 4, 5, 6, 7, 8, 9]

交差点

List<String> firstList = Arrays.asList("A", "B", "C", "D", "E", "F");
List<String> secondList = Arrays.asList("B", "D", "F", "G", "H", "K");

//intersections of two sets
Collection<String> result = CollectionUtils.intersection(firstList, secondList);
System.out.println(ArrayUtils.toString(result));     //[B, D, F]

論理和

List<String> firstList = Arrays.asList("1", "2", "3", "4", "5", "6");
List<String> secondList = Arrays.asList("2", "3", "6", "7", "8", "9");

//disjunction
Collection<String> result = CollectionUtils.disjunction(firstList, secondList);
System.out.println(ArrayUtils.toString(result)); //[1, 4, 5, 7, 8, 9]

差額(控除)

List<String> firstList = Arrays.asList("1", "2", "3", "4", "5", "6");
List<String> secondList = Arrays.asList("2", "3", "6", "7", "8", "9");

//difference
Collection<String> result = CollectionUtils.subtract(firstList, secondList);
System.out.println(ArrayUtils.toString(result)); // [1, 4, 5]

セットの操作 (続き)

この例では、上記の 4 つのメソッドを使用して 2 つのコレクションを操作する方法を示します。

List<Integer> firstList = Arrays.asList(1, 2, 3, 3, 4, 5);
List<Integer> secondList = Arrays.asList(3, 4, 4, 5, 6, 7);

Collection<Integer> union = CollectionUtils.union(firstList, secondList); // An association
Collection<Integer> intersection = CollectionUtils.intersection(firstList, secondList); // Intersection
Collection<Integer> disjunction = CollectionUtils.disjunction(firstList, secondList); // disjunction
Collection<Integer> subtract = CollectionUtils.subtract(firstList, secondList); // Difference

System.out.println("firstList: " + ArrayUtils.toString(firstList.toArray()));
System.out.println("secondList: " + ArrayUtils.toString(secondList.toArray()));

System.out.println("An association: " + ArrayUtils.toString(union.toArray()));
System.out.println("Intersection: " + ArrayUtils.toString(intersection.toArray()));
System.out.println("Disjunction: " + ArrayUtils.toString(disjunction.toArray()));
System.out.println("Difference: " + ArrayUtils.toString(subtract.toArray()));

私たちの結果:

firstList: {1,2,3,3,4,5}
secondList: {3,4,4,5,6,7}
An association: {1,2,3,3,4,4,5,6,7}
intersection: {3,4,5}
Disjunction: {1,2,3,4,6,7}
Difference: {1,2,3}

これらの方法を自分で試すことができます。

unmodifiableCollection() メソッド

場合によっては、オブジェクトの内部データのコレクションを返す必要があるインターフェイス メソッドを実装する必要があります。データは内部にあるため、誰かがどこかで変更することは望ましくありません。

または、変更する必要がないコレクションをどこかで受け取ったが、それをサードパーティのメソッドに強制的に渡す必要があります。彼がそれを変えないという保証はどこにあるのでしょうか?

安らかにスリープするために、コレクションを変更しようとしたときに例外をスローする特別なラッパーでコレクションをラップすることができます。

例:

Collection<String> firstCollection = new ArrayList<String>();
Collection<String> secondCollection = Collections.unmodifiableCollection(firstCollection);
firstCollection.add("hello");
firstCollection.add("from");
firstCollection.add("codeGym");

//secondCollection.add("have a error");
System.out.println(secondCollection); //[hello, from, codeGym]

Collections.unmodifiableCollection()メソッドは、渡されたコレクションのラッパーを返します。get()size()メソッドを呼び出すと、すべてが機能します。ただし、add()set()remove()メソッドを呼び出すと、例外が発生します。

java.lang.UnsupportedOperationException
at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)

実際、このメソッドはすでに非推奨としてマークされており、代わりにCollections.unmodifiableCollection(Collection<? extends T> c)を使用することをお勧めします。

Collection<String> firstCollection = new ArrayList<String>();
Collection<String> secondCollection =
Collections.unmodifiableCollection(firstCollection);