Apache 的CollectionUtils各种集合操作好用的方法总结

CollectionUtils 是 Apache Commons Collections 提供的一个工具类,包含了许多静态方法,用于操作和处理集合。以下是一些常用的方法介绍及其使用示例:

1. isEmpty(Collection coll)

检查集合是否为空。

java 复制代码
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.List;

List<String> list = new ArrayList<>();
boolean isEmpty = CollectionUtils.isEmpty(list); // true

2. isNotEmpty(Collection coll)

检查集合是否非空。

java 复制代码
List<String> list = new ArrayList<>();
list.add("item");
boolean isNotEmpty = CollectionUtils.isNotEmpty(list); // true

3. addIgnoreNull(Collection<?> coll, Object obj)

向集合中添加元素,忽略 null 值。

java 复制代码
List<String> list = new ArrayList<>();
CollectionUtils.addIgnoreNull(list, null); // 不会添加
CollectionUtils.addIgnoreNull(list, "item"); // 添加 'item'

4. union(Collection<?> coll1, Collection<?> coll2)

返回两个集合的并集,结果中不包含重复元素。

java 复制代码
List<String> list1 = List.of("a", "b", "c");
List<String> list2 = List.of("b", "c", "d");
Collection<String> union = CollectionUtils.union(list1, list2); 
// union: ["a", "b", "c", "d"]

//union 方法返回两个集合的并集,重复元素只保留一次。
List<String> list1 = List.of("a", "b", "b", "c");
List<String> list2 = List.of("b", "c", "d", "d");

Collection<String> union = CollectionUtils.union(list1, list2); 
// union: ["a", "b", "c", "d"] (只保留一次)
System.out.println(union); // 输出: ["a", "b", "c", "d"]

5. intersection(Collection<?> coll1, Collection<?> coll2)

返回两个集合的交集。

java 复制代码
List<String> list1 = List.of("a", "b", "c");
List<String> list2 = List.of("b", "c", "d");
Collection<String> intersection = CollectionUtils.intersection(list1, list2); 
// intersection: ["b", "c"]


//intersection 方法返回两个集合的交集,重复元素会根据出现的最小数量保留。
List<String> list1 = List.of("a", "b", "b", "c");
List<String> list2 = List.of("b", "c", "c", "d");

Collection<String> intersection = CollectionUtils.intersection(list1, list2); 
// intersection: ["b", "c"] (没有重复元素)
System.out.println(intersection); // 输出: ["b", "c"]

6. disjunction(Collection<?> coll1, Collection<?> coll2)

返回两个集合的对称差集(即只在一个集合中存在的元素)。

java 复制代码
List<String> list1 = List.of("a", "b", "c");
List<String> list2 = List.of("b", "c", "d");
Collection<String> disjunction = CollectionUtils.disjunction(list1, list2);
// disjunction: ["a", "d"]


// disjunction: ["a", "d"] (重复元素 d 也只保留一次)
List<String> list1 = List.of("a", "b", "b", "c");
List<String> list2 = List.of("b", "c", "d", "d");

Collection<String> disjunction = CollectionUtils.disjunction(list1, list2); 
// disjunction: ["a", "d"] (重复元素 d 也只保留一次)
System.out.println(disjunction); // 输出: ["a", "d"]

7. transform(Collection<?> collection, Transformer transformer)

对集合中的每个元素应用给定的转换器。

java 复制代码
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.CollectionUtils;

List<String> list = List.of("1", "2", "3");
Transformer<String, Integer> transformer = new Transformer<>() {
    public Integer transform(String input) {
        return Integer.valueOf(input);
    }
};
Collection<Integer> transformed = CollectionUtils.collect(list, transformer);
// transformed: [1, 2, 3]

8. forAllDo(Collection<?> collection, Closure closure)

对集合中的每个元素应用给定的闭包(执行一段操作)。

java 复制代码
import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;

List<String> list = List.of("Item1", "Item2");
Closure<String> closure = new Closure<>() {
    public void execute(String input) {
        System.out.println(input); // 打印每个元素
    }
};
CollectionUtils.forAllDo(list, closure); // 输出 Item1 和 Item2

9. select(Collection<?> collection, Predicate predicate)

根据给定的条件选择满足条件的元素。

java 复制代码
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.CollectionUtils;

List<Integer> numbers = List.of(1, 2, 3, 4, 5);
Predicate<Integer> evenPredicate = num -> num % 2 == 0;
Collection<Integer> evens = CollectionUtils.select(numbers, evenPredicate);
// evens: [2, 4]

10. partition(Collection<?> collection, Predicate predicate)

将集合分割成两个部分,根据给定的条件。

java 复制代码
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.CollectionUtils;

List<Integer> numbers = List.of(1, 2, 3, 4, 5);
Predicate<Integer> evenPredicate = num -> num % 2 == 0;
List<Collection<Integer>> partitioned = CollectionUtils.partition(numbers, evenPredicate);

// partitioned: [ [2, 4], [1, 3, 5] ]
System.out.println(partitioned);

11. transformIntoList(Collection collection, Transformer<T, R> transformer)

将集合中的元素转换为列表。

java 复制代码
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.CollectionUtils;

List<String> list = List.of("1", "2", "3");
Transformer<String, Integer> transformer = Integer::valueOf;
List<Integer> transformedList = CollectionUtils.collect(list, transformer);

// transformedList: [1, 2, 3]
System.out.println(transformedList);

12. collate(Collection<?> collection1, Collection<?> collection2)

将两个集合拼接成一个新的集合。

java 复制代码
List<String> list1 = List.of("a", "b");
List<String> list2 = List.of("c", "d");
Collection<String> collated = CollectionUtils.union(list1, list2);
// collated: ["a", "b", "c", "d"]

//collate 方法将两个集合拼接成一个新的集合,但需要注意,它不是 set 操作,不会去重。
List<String> list1 = List.of("a", "b", "b", "c");
List<String> list2 = List.of("c", "d", "d");

Collection<String> collated = CollectionUtils.union(list1, list2);
// collated: ["a", "b", "b", "c", "c", "d", "d"]
System.out.println(collated); // 输出: ["a", "b", "b", "c", "c", "d", "d"]

13. filter(Collection<?> collection, Predicate predicate)

根据给定的条件过滤集合中的元素。

java 复制代码
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
Predicate<Integer> greaterThanTwo = num -> num > 2;
Collection<Integer> filtered = CollectionUtils.select(numbers, greaterThanTwo);
// filtered: [3, 4, 5]

14. containsAny(Collection<?> coll1, Collection<?> coll2)

检查两个集合中是否有任何元素相同。

java 复制代码
List<String> list1 = List.of("a", "b", "c");
List<String> list2 = List.of("c", "d", "e");
boolean containsAny = CollectionUtils.containsAny(list1, list2); // true

List<String> list1 = List.of("a", "b", "b", "c");
List<String> list2 = List.of("c", "d", "d");

boolean containsAny = CollectionUtils.containsAny(list1, list2); // true
System.out.println(containsAny); // 输出: true

15. isEqualCollection(Collection<?> coll1, Collection<?> coll2)

检查两个集合是否相等,考虑到元素的数量。

java 复制代码
List<String> list1 = List.of("a", "b", "c");
List<String> list2 = List.of("a", "b", "c");
boolean areEqual = CollectionUtils.isEqualCollection(list1, list2); // true

//isEqualCollection 方法判断两个集合是否相等(不考虑顺序但考虑数量)。
//重复、数量一样但顺序不一样
List<String> list1 = Arrays.asList("a", "b", "b", "c");
List<String> list2 = Arrays.asList("a", "b", "c", "b");

boolean areEqual = CollectionUtils.isEqualCollection(list1, list2); // true
System.out.println(areEqual); // 输出: true

//重复但是数量不一
List<String> list3 = Arrays.asList("a", "b", "b", "c");
List<String> list4 = Arrays.asList("a", "b", "c", "c");

boolean areEqual2 = CollectionUtils.isEqualCollection(list4, list3); // false
System.out.println(areEqual2); // 输出: false

16. subtract(Collection<?> coll1, Collection<?> coll2)

返回一个集合中存在而另一个集合中不存在的元素。

java 复制代码
List<String> list1 = List.of("a", "b", "c", "d");
List<String> list2 = List.of("b", "c");
Collection<String> difference = CollectionUtils.subtract(list1, list2); // ["a", "d"]

//subtract 方法返回从第一个集合中减去第二个集合的结果,重复元素会根据数量进行减法。
List<String> list1 = new ArrayList<>(List.of("a", "b", "b", "c"));
List<String> list2 = List.of("b", "d");

Collection<String> difference = CollectionUtils.subtract(list1, list2); 
// difference: ["a", "b", "c"] (只保留第一次出现的 b)
System.out.println(difference); // 输出: ["a", "b", "c"]

17. reverseList(List<?> list)

反转列表的顺序。

java 复制代码
import java.util.ArrayList;
import java.util.List;

List<String> list = new ArrayList<>(List.of("a", "b", "c"));
CollectionUtils.reverseList(list);
// list: ["c", "b", "a"]

18. combine(Collection<?> coll1, Collection<?> coll2)

将两个集合合并成一个新的集合。

java 复制代码
List<String> list1 = List.of("1", "2");
List<String> list2 = List.of("3", "4");
Collection<String> combined = CollectionUtils.union(list1, list2);
// combined: ["1", "2", "3", "4"]

combine 方法实际上可以使用 union,返回合并后的集合,重复数据只保留一次。
List<String> list1 = List.of("a", "b", "b", "c");
List<String> list2 = List.of("b", "c", "d", "d");

Collection<String> combined = CollectionUtils.union(list1, list2);
// combined: ["a", "b", "c", "d"]
System.out.println(combined); // 输出: ["a", "b", "c", "d"]

19. retainAll(Collection<?> coll1, Collection<?> coll2)

保留集合中仅在两个集合中都存在的元素。

java 复制代码
List<String> list1 = new ArrayList<>(List.of("a", "b", "c"));
List<String> list2 = List.of("b", "c", "d");
CollectionUtils.retainAll(list1, list2); // list1 现在变为 ["b", "c"]

//retainAll 方法保留集合中仅在两个集合中都存在的元素,考虑重复数据。
List<String> list1 = new ArrayList<>(List.of("a", "b", "b", "c"));
List<String> list2 = new ArrayList<>(List.of("b", "c", "c", "d"));

list1.retainAll(list2);
// list1 现在变为 ["b", "b", "c"]
System.out.println(list1); // 输出: ["b", "b", "c"]
相关推荐
鸠摩智首席音效师2 小时前
如何在 Apache 中创建单个文件的别名 ?
apache
Hello.Reader1 天前
深入解析 Apache APISIX
java·apache
hwscom1 天前
如何永久解决Apache Struts文件上传漏洞
java·服务器·struts·web安全·apache
白开水2331 天前
Apache RocketMQ 5.1.3安装部署文档
apache·rocketmq
s甜甜的学习之旅2 天前
Apache POI练习代码
apache
是小崔啊2 天前
开源轮子 - Apache Common
java·开源·apache
程序猿阿伟3 天前
《探索 Apache Spark MLlib 与 Java 结合的卓越之道》
java·spark-ml·apache
开心工作室_kaic3 天前
springboot461学生成绩分析和弱项辅助系统设计(论文+源码)_kaic
开发语言·数据库·vue.js·php·apache
cr.sheeper3 天前
Vulnhub靶场Apache解析漏洞
网络安全·apache
ccc_9wy4 天前
Apache Solr RCE(CVE-2017-12629)--vulhub
apache·solr·lucene·burp suite·vulhub·远程命令执行漏洞rce·cve-2017-12629