目录
- 前言
- 1. 集合交并差
- 2. CRUD
- 3. 常用
前言
- java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)
- 【Java项目】实战CRUD的功能整理(持续更新)
CollUtil 是一个工具类,通常用于集合的操作,提供了多种便捷的方法来处理集合,例如添加、删除、查找、转换等
1. 集合交并差
对应的API展示如下:
| 方法名 | 说明 | 示例 | 
|---|---|---|
| CollUtil.addAll(Collection<? super T> coll, T… elems) | 向集合中添加多个元素 | CollUtil.addAll(list, Arrays.asList(“元素1”, “元素2”)); | 
| CollUtil.isEmpty(Collection<?> coll) | 检查集合是否为空 | boolean isEmpty = CollUtil.isEmpty(list); | 
| CollUtil.join(Collection<?> coll, String separator) | 将集合元素连接成字符串 | String result = CollUtil.join(list, ", "); | 
| CollUtil.union(Collection<?> coll1, Collection<?> coll2) | 计算两个集合的并集(去重) | Collection union = CollUtil.union(list1, list2); | 
| CollUtil.unionDistinct(Collection<?> coll1, Collection<?> coll2) | 计算多个集合的并集,去重 | Collection unionDistinct = CollUtil.unionDistinct(list1, list2); | 
| CollUtil.unionAll(Collection<?> coll1, Collection<?> coll2) | 计算多个集合的并集,不去重 | List unionAll = CollUtil.unionAll(list1, list2); | 
| CollUtil.intersection(Collection<?> coll1, Collection<?> coll2) | 计算两个集合的交集 | Collection intersection = CollUtil.intersection(list1, list2); | 
| CollUtil.intersection(Collection<?> coll1, Collection<?> coll2, Collection<?>… otherColls) | 计算多个集合的交集 | Collection intersectionMultiple = CollUtil.intersection(list1, list2, list3); | 
| CollUtil.subtract(Collection<?> coll1, Collection<?> coll2) | 计算集合的差集 | Collection difference = CollUtil.subtract(list1, list2); | 
| CollUtil.disjunction(Collection<?> coll1, Collection<?> coll2) | 计算两个集合的对称差集 | Collection disjunction = CollUtil.disjunction(list1, list2); | 
| emptyIfNull(Set set) | 如果集合为空,返回一个空的不可变集合 | Set safeSet = emptyIfNull(nullSet); | 
| emptyIfNull(List list) | 如果列表为空,返回一个空的不可变列表 | List safeList = emptyIfNull(nullList); | 
import cn.hutool.core.collection.CollUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static cn.hutool.core.collection.CollUtil.emptyIfNull;
public class CollUtilDemo {
    public static void main(String[] args) {
        // 创建一个空的列表
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>(Arrays.asList("元素2", "元素2", "元素3", "元素4", "元素5"));
        // 1. 添加多个元素到集合中
        CollUtil.addAll(list1, Arrays.asList("元素1", "元素2")); // 使用 Arrays.asList
        System.out.println("添加元素后列表1: " + list1); // 输出: [元素1, 元素2]
        // 2. 检查集合是否为空
        boolean isEmpty = CollUtil.isEmpty(list1);
        System.out.println("列表1是否为空: " + isEmpty); // 输出: false
        // 3. 将集合元素连接成字符串
        String result = CollUtil.join(list1, ", ");
        System.out.println("连接后的字符串: " + result); // 输出: 元素1, 元素2
        // 4. 合并集合,得到并集
        Collection<String> union = CollUtil.union(list1, list2);
        System.out.println("并集: " + union); // 输出: [元素1, 元素2, 元素2, 元素3, 元素4, 元素5]
        // 5. 获取多个集合的并集(去重)
        Collection<String> unionDistinct = CollUtil.unionDistinct(list1, list2);
        System.out.println("并集(去重): " + unionDistinct); // 输出: [元素1, 元素2, 元素3, 元素4, 元素5]
        // 6. 不去重的并集
        List<String> unionAll = CollUtil.unionAll(list1, list2);
        System.out.println("不去重的并集: " + unionAll); // 输出: [元素1, 元素2, 元素2, 元素2, 元素3, 元素4, 元素5]
        // 7. 计算交集
        Collection<String> intersection = CollUtil.intersection(list1, list2);
        System.out.println("交集: " + intersection); // 输出: [元素2]
        // 8. 计算多个集合的交集
        List<String> list3 = new ArrayList<>(Arrays.asList("元素1", "元素3"));
        Collection<String> intersectionMultiple = CollUtil.intersection(list1, list2, list3);
        System.out.println("多个集合的交集: " + intersectionMultiple); // 输出: []
        // 9. 计算集合的差集
        Collection<String> difference = CollUtil.subtract(list1, list2);
        System.out.println("差集: " + difference); // 输出: [元素1]
        // 10. 计算两个集合的对称差(不在两个集合中的元素)
        Collection<String> disjunction = CollUtil.disjunction(list1, list2);
        System.out.println("对称差集: " + disjunction); // 输出: [元素1, 元素2, 元素3, 元素4, 元素5]
        // 11. 使用 emptyIfNull 方法检查集合是否为空
        List<String> nullList = null;
        List<String> safeList = emptyIfNull(nullList);
        System.out.println("空列表处理: " + safeList); // 输出: []
    }
}
截图如下:

2. CRUD
| 方法名 | 说明 | 示例 | 
|---|---|---|
| CollUtil.contains(Collection<?> collection, Object value) | 检查集合中是否包含指定元素 | boolean contains = CollUtil.contains(collection, “元素1”); | 
| CollUtil.safeContains(Collection<?> collection, Object value) | 安全地检查集合中是否包含指定元素 | boolean safeContains = CollUtil.safeContains(collection, “元素4”); | 
| CollUtil.contains(Collection collection, Predicate<? super T> containFunc) | 使用 Predicate 检查集合是否包含满足条件的元素 | boolean containsPredicate = CollUtil.contains(collection, elem -> elem.equals(“元素2”)); | 
| CollUtil.containsAny(Collection<?> coll1, Collection<?> coll2) | 检查两个集合是否有共同的元素 | boolean containsAny = CollUtil.containsAny(collection, otherCollection); | 
| CollUtil.containsAll(Collection<?> coll1, Collection<?> coll2) | 检查一个集合是否包含另一个集合的所有元素 | boolean containsAll = CollUtil.containsAll(collection, Arrays.asList(“元素1”, “元素2”)); | 
| CollUtil.countMap(Iterable collection) | 计算集合中元素的出现次数 | Map<String, Integer> countMap = CollUtil.countMap(Arrays.asList(“元素1”, “元素2”, “元素1”, “元素3”)); | 
| CollUtil.join(Iterable iterable, CharSequence conjunction) | 将集合元素连接成字符串 | String joined = CollUtil.join(collection, ", "); | 
| CollUtil.join(Iterable iterable, CharSequence conjunction, String prefix, String suffix) | 使用前后缀连接集合元素 | String joinedWithPrefixSuffix = CollUtil.join(collection, ", ", “[”, “]”); | 
| CollUtil.popPart(Stack surplusAlaDatas, int partSize) | 从 Stack 中弹出部分元素 | List poppedStack = CollUtil.popPart(stack, 2); | 
| CollUtil.popPart(Deque surplusAlaDatas, int partSize) | 从 Deque 中弹出部分元素 | List poppedDeque = CollUtil.popPart(deque, 2); | 
import cn.hutool.core.collection.CollUtil;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
public class CollUtilExtendedDemo {
    public static void main(String[] args) {
        // 创建一个集合
        Collection<String> collection = new ArrayList<>(Arrays.asList("元素1", "元素2", "元素3"));
        // 1. 检查集合中是否包含指定元素
        boolean contains = CollUtil.contains(collection, "元素1");
        System.out.println("集合是否包含 '元素1': " + contains); // 输出: true
        // 2. 安全地检查集合中是否包含指定元素
        boolean safeContains = CollUtil.safeContains(collection, "元素4");
        System.out.println("集合是否安全地包含 '元素4': " + safeContains); // 输出: false
        // 3. 使用 Predicate 检查集合是否包含满足条件的元素
        boolean containsPredicate = CollUtil.contains(collection, elem -> elem.equals("元素2"));
        System.out.println("集合是否包含满足条件的元素 '元素2': " + containsPredicate); // 输出: true
        // 4. 检查两个集合是否有共同的元素
        Collection<String> otherCollection = Arrays.asList("元素3", "元素4");
        boolean containsAny = CollUtil.containsAny(collection, otherCollection);
        System.out.println("集合是否包含其他集合中的任何元素: " + containsAny); // 输出: true
        // 5. 检查一个集合是否包含另一个集合的所有元素
        boolean containsAll = CollUtil.containsAll(collection, Arrays.asList("元素1", "元素2"));
        System.out.println("集合是否包含所有指定元素: " + containsAll); // 输出: true
        // 6. 计算集合中元素的出现次数
        Map<String, Integer> countMap = CollUtil.countMap(Arrays.asList("元素1", "元素2", "元素1", "元素3", "元素2", "元素2"));
        System.out.println("元素出现次数: " + countMap); // 输出: {元素1=2, 元素2=3, 元素3=1}
        // 7. 连接集合元素成字符串
        String joined = CollUtil.join(collection, ", ");
        System.out.println("连接后的字符串: " + joined); // 输出: 元素1, 元素2, 元素3
        // 8. 使用前后缀连接集合元素
        String joinedWithPrefixSuffix = CollUtil.join(collection, ", ", "[", "]");
        System.out.println("带前后缀的连接字符串: " + joinedWithPrefixSuffix); // 输出: [元素1, 元素2, 元素3]
        // 9. 从 Stack 中弹出部分元素
        Stack<String> stack = new Stack<>();
        stack.push("元素A");
        stack.push("元素B");
        stack.push("元素C");
        List<String> poppedStack = CollUtil.popPart(stack, 2);
        System.out.println("从 Stack 弹出的元素: " + poppedStack); // 输出: [元素C, 元素B]
        System.out.println("Stack 剩余元素: " + stack); // 输出: [元素A]
        // 10. 从 Deque 中弹出部分元素
        Deque<String> deque = new LinkedList<>();
        deque.add("元素X");
        deque.add("元素Y");
        deque.add("元素Z");
        List<String> poppedDeque = CollUtil.popPart(deque, 2);
        System.out.println("从 Deque 弹出的元素: " + poppedDeque); // 输出: [元素Z, 元素Y]
        System.out.println("Deque 剩余元素: " + deque); // 输出: [元素X]
    }
}
截图如下:

对应删除的API如下:
| 方法名 | 说明 | 输出示例 | 
|---|---|---|
| removeNull(T collection) | 移除集合中的空值 | 移除空值后的集合: [元素1, , 元素2, , 元素3, 元素4] | 
| removeEmpty(T collection) | 移除集合中的空字符串 | 移除空字符串后的集合: [元素1, 元素2, , 元素3, 元素4] | 
| removeBlank(T collection) | 移除集合中的空白字符串 | 移除空白字符串后的集合: [元素1, 元素2, 元素3, 元素4] | 
| removeWithAddIf(T targetCollection, Predicate<? super E> predicate) | 根据条件移除元素并添加到目标集合 | 移除包含’A’的元素后目标集合: [元素B, 元素C, 元素D] | 
| 移除的元素: [元素A] | ||
| extract(Iterable<?> collection, Editor editor) | 提取集合中的元素,并进行转换 | 提取并转换后的集合: [元素1, , 元素2, null, , 元素3, 元素4] | 
import cn.hutool.core.collection.CollUtil;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class CollUtilDemo1 {
    public static void main(String[] args) {
        // 创建一个包含空值和空字符串的集合
        List<String> collection = new ArrayList<>(Arrays.asList("元素1", "", "元素2", null, " ", "元素3", "元素4"));
        // 1. 移除集合中的空值
        List<String> noNulls = CollUtil.removeNull(collection);
        System.out.println("移除空值后的集合: " + noNulls); // 输出: [元素1, , 元素2,  , 元素3, 元素4]
        // 2. 移除集合中的空字符串
        List<String> noEmptyStrings = CollUtil.removeEmpty(noNulls);
        System.out.println("移除空字符串后的集合: " + noEmptyStrings); // 输出: [元素1, 元素2,  , 元素3, 元素4]
        // 3. 移除集合中的空白字符串
        List<String> noBlankStrings = CollUtil.removeBlank(noEmptyStrings);
        System.out.println("移除空白字符串后的集合: " + noBlankStrings); // 输出: [元素1, 元素2, 元素3, 元素4]
        // 4. 根据条件移除元素并添加到目标集合
        List<String> targetCollection = new ArrayList<>(Arrays.asList("元素A", "元素B", "元素C", "元素D"));
        List<String> resultCollection = new ArrayList<>();
        CollUtil.removeWithAddIf(targetCollection, resultCollection, s -> s.contains("A"));
        System.out.println("移除包含'A'的元素后目标集合: " + targetCollection); // 输出: [元素B, 元素C, 元素D]
        System.out.println("移除的元素: " + resultCollection); // 输出: [元素A]
        // 5. 从集合中提取元素
        List<Object> extracted = CollUtil.extract(collection, obj -> obj.toString().toUpperCase());
        System.out.println("提取并转换后的集合: " + extracted); // 输出: [元素1, , 元素2, null,  , 元素3, 元素4]
    }
}
截图如下:

3. 常用
import cn.hutool.core.collection.CollUtil;
import java.util.ArrayList;
import java.util.List;
public class CollUtilTotalDemo {
    public static void main(String[] args) {
        // 1. 判断是否为空
        List<String> list1 = null;
        boolean isEmpty = CollUtil.isEmpty(list1); // true
        System.out.println("list1 为空: " + isEmpty);
        // 2. 判断是否非空
        List<String> list2 = new ArrayList<>();
        boolean isNotEmpty = CollUtil.isNotEmpty(list2); // false
        System.out.println("list2 非空: " + isNotEmpty);
        // 3. 集合是否包含某个元素
        List<String> list3 = CollUtil.newArrayList("a", "b", "c");
        boolean contains = CollUtil.contains(list3, "a"); // true
        System.out.println("list3 包含 'a': " + contains);
        // 4. 集合是否包含某些元素
        boolean containsAll = CollUtil.containsAll(list3, CollUtil.newArrayList("a", "b")); // true
        System.out.println("list3 包含 'a' 和 'b': " + containsAll);
        // 5. 两个集合的交集操作
        List<String> list4 = CollUtil.newArrayList("a", "b", "c");
        List<String> list5 = CollUtil.newArrayList("b", "c", "d");
        List<String> intersection = new ArrayList<>(CollUtil.intersection(list4, list5)); // ["b", "c"]
        System.out.println("list4 和 list5 的交集: " + intersection);
        // 6. 两个集合的并集操作
        List<String> union = new ArrayList<>(CollUtil.union(list4, list5)); // ["a", "b", "c", "d"]
        System.out.println("list4 和 list5 的并集: " + union);
        // 7. 两个集合的差集操作
        List<String> subtract = new ArrayList<>(CollUtil.subtract(list4, list5)); // ["a"]
        System.out.println("list4 和 list5 的差集: " + subtract);
        // 8. 集合转换为字符串,列表转为逗号分隔的字符串
        String join = CollUtil.join(list3, ", "); // "a, b, c"
        System.out.println("list3 转为字符串: " + join);
    }
}
截图如下:
















![[大语言模型-论文精读] 悉尼大学-ACL2024-提升大型语言模型的复杂视觉推理能力](https://i-blog.csdnimg.cn/direct/a5c9dc8458d04f45be382b2057332c65.png)



