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