文章目录
一、介绍
Hutool是一个Java工具类库,它简化了Java的开发,提供了很多实用的API。在Hutool中,对于集合(Collection)的操作提供了很多便捷的方法。
二、Hutool集合操作示例
1、判断是否为空:
List<String> list = null;
boolean isEmpty = CollUtil.isEmpty(list); // true
2、判断是否非空
List<String> list = new ArrayList<>();
boolean isNotEmpty = CollUtil.isNotEmpty(list); // false
3、集合是否包含某个元素
List<String> list = CollUtil.newArrayList("a", "b", "c");
boolean contains = CollUtil.contains(list, "a"); // true
4、集合是否包含某些元素
List<String> list = CollUtil.newArrayList("a", "b", "c");
boolean containsAll = CollUtil.containsAll(list, CollUtil.newArrayList("a", "b")); // true
5、集合是否不包含某些元素
List<String> list = CollUtil.newArrayList("a", "b", "c");
boolean notContains = CollUtil.notContains(list, "d"); // true
6、两个集合的交集操作
List<String> list1 = CollUtil.newArrayList("a", "b", "c");
List<String> list2 = CollUtil.newArrayList("b", "c", "d");
List<String> intersection = CollUtil.intersection(list1, list2); // ["b", "c"]
7、两个集合的并集操作
List<String> list1 = CollUtil.newArrayList("a", "b", "c");
List<String> list2 = CollUtil.newArrayList("b", "c", "d");
List<String> union = CollUtil.union(list1, list2); // ["a", "b", "c", "d"]
8、两个集合的差集操作
List<String> list1 = CollUtil.newArrayList("a", "b", "c");
List<String> list2 = CollUtil.newArrayList("b", "c", "d");
List<String> subtract = CollUtil.subtract(list1, list2); // ["a"]
9、集合元素是否相等
List<String> list1 = CollUtil.newArrayList("a", "b", "c");
List<String> list2 = CollUtil.newArrayList("a", "b", "c");
boolean equals = CollUtil.equals(list1, list2); // true
10、集合转换为字符串,列表转为逗号分隔的字符串
List<String> list = CollUtil.newArrayList("a", "b", "c");
String join = CollUtil.join(list, ", "); // "a, b, c"
标签:Java,list1,newArrayList,CollUtil,List,hutool,list,集合
From: https://blog.csdn.net/yuming226/article/details/137225322