详细分析CollUtil的基本知识(附Demo)

前言

  1. java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)
  2. 【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);
    }
}

截图如下:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农研究僧

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值