CollectionUtils 簡介

另一個包含許多有用方法的通用實用程序類:

addAll(Collection<C> 集合, C...元素) 將數組 C 的所有元素添加到集合中
addIgnoreNull(Collection<T> 集合,T 對象) 添加一個元素,如果它不為空
包含所有(集合<?> coll1,集合<?> coll2) 檢查 collection1 是否包含 collection2 的所有元素
containsAny(集合<?> coll1,集合<?> coll2) 檢查 collection1 是否至少包含 collection2 中的一個元素
countMatches(Iterable<C> 輸入, Predicate<? super C> 謂詞) 計算集合中有多少元素滿足謂詞規則
析取(可迭代<?擴展O> a,可迭代<?擴展O> b) 返回僅在一個集合中但不在兩個集合中的對象
交集(可迭代<?擴展O> a,可迭代<?擴展O> b) 返回同時屬於兩個集合的對象集合
聯合(可迭代<?擴展O> a,可迭代<?擴展O> b) 返回一組對象,這些對象至少屬於一個集合
空集合() 返回一個特殊的空集合
emptyIfNull(集合 <T> 集合) 如果 collection == null 則返回一個空集合
存在(Iterable<C> 輸入,Predicate<? super C> 謂詞) 檢查集合中是否存在滿足謂詞規則的元素
extractSingleton(集合<E>集合) 返回集合的唯一元素
filter(Iterable<T> collection, Predicate<? super T> predicate) 過濾集合的元素
find(Iterable<T> collection, Predicate<? super T> 謂詞) 尋找集合的元素
isEmpty(集合<?> coll) 檢查集合是否為空
isEqualCollection(集合<?> a,集合<?> b) 比較集合
isFull(Collection<? extends Object> coll) 檢查元素是否仍然可以添加到集合中
isNotEmpty(集合<?> coll) 檢查集合是否不為空
isSubCollection(集合<?> a,集合<?> b) 檢查集合 B 是否在集合 A 中
matchesAll(Iterable<C> 輸入, Predicate<? super C> 謂詞) 檢查集合的所有元素是否滿足謂詞規則
removeAll(集合<E> 集合, 集合<?> 移除) 移除集合中的所有元素
retainAll(集合<C> 集合, 集合<?> 保留) 返回包含集合中所有元素的集合,這些元素也包含在保留中
reverseArray(對象[]數組) 向後反轉數組
unmodifiableCollection(Collection<? extends C> collection) 返回集合的包裝器。任何修改集合的嘗試都將引發異常。

讓我們看一些方法。

檢查集合是否為空的方法

以下是確定集合是否為空的簡單方法:

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 集合指南 CollectionUtils

集上的操作

如果你必須處理集合論,那麼你就會知道集合有 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]

集上的操作,續

此示例顯示如何使用上述四種方法來處理兩個集合:

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);