欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Apache 的CollectionUtils各种集合操作好用的方法总结

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

2024/10/26 0:28:15 来源:https://blog.csdn.net/u014745465/article/details/142136588  浏览:    关键词:Apache 的CollectionUtils各种集合操作好用的方法总结

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

1. isEmpty(Collection coll)

检查集合是否为空。

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)

检查集合是否非空。

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

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

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

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

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

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

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)

返回两个集合的交集。

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

List<String> list1 = List.of("a", "b", "c");
List<String> list2 = List.of("c", "d", "e");
boolean containsAny = CollectionUtils.containsAny(list1, list2); // trueList<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)

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

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)

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

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)

反转列表的顺序。

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)

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

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)

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

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"]

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com