【工具类】常用的工具类——CollectionUtil

news2025/5/24 13:13:01

目录

    • cn.hutool.core.collection.CollectionUtil
      • 集合创建
      • 集合清空
      • 集合判空
      • 集合去重
      • 集合过滤
      • 集合转换
      • 集合合并
      • 集合交集
      • 集合差集
      • 集合是否包含元素
      • 集合是否包含指定元素(自定义条件)
      • 集合分页
      • 集合分组
      • 集合转字符串
      • 元素添加
      • 元素删除
      • 根据属性转Map
      • 获取元素
      • 获取最大/最小元素
      • 集合排序

cn.hutool.core.collection.CollectionUtil

集合创建

List<User> userList = CollectionUtil.list(true);
//true:ArrayList,false:LinkedList

List<String> list1 = Arrays.asList("1", "2", "3", "4", "5");//不可变集合
List<String> list2 = CollectionUtil.toList("1", "2", "3", "4", "5");//可变集合
System.out.println(list2);//[1, 2, 3, 4,5]

集合清空

List<String> list = CollectionUtil.toList("1", "2", "3", "4", "5");
CollectionUtil.clear(list);
System.out.println(list);//[]

集合判空

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

List<String> list = Arrays.asList("a", "b", "c");
boolean isNotEmpty = CollectionUtils.isNotEmpty(list); // true

List<String> list1 = null;
List<String> list2 = Arrays.asList("1","2","3");
List<String> result1 = CollectionUtil.emptyIfNull(list1);//[]
List<String> result2 = CollectionUtil.emptyIfNull(list2);//["1","2","3"]

Set<String> set1 = null;
Set<String> set2 = new HashSet<>(Arrays.asList("1","2","3"));
Set<String> result3 = CollectionUtil.emptyIfNull(set1);//[]
Set<String> result4 = CollectionUtil.emptyIfNull(set2);//["1","2","3"]

//空集合给默认值
List<String> list = new ArrayList<>();
List<String> result = CollectionUtil.defaultIfEmpty(list, Arrays.asList("1", "2", "3"));//["1", "2", "3"]

//判断是否有空元素
CollectionUtil.hasNull(list);

集合去重

List<String> list = Arrays.asList("a", "b", "a", "c");
List<String> distinctList = CollectionUtil.distinct(list); // ["a", "b", "c"]

集合过滤

//List<String> list =Arrays.asList("a", "b", "c");//Arrays.asList()返回的集合是不可变长度的,在这里这么用会报错:UnsupportedOperationException
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> filteredList = CollectionUtil.filter(list, e -> e.equals("a")); // ["a"]

集合转换

List<String> list = Arrays.asList("1", "2", "3");
List<Integer> mappedList = CollectionUtil.map(list, Integer::valueOf,true); // [1, 2, 3]

集合合并

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

Set<String> unionDistinctList = CollectionUtil.unionDistinct(list1, list2); // ["a", "b", "c"]

List<String> unionAllList = (List<String>)CollectionUtil.unionAll(list1, list2); // ["a", "b", "c"]

集合交集

List<String> list1 = Arrays.asList("a", "b");
List<String> list2 = Arrays.asList("b", "c");
List<String> intersectionList = (List<String>)CollectionUtil.intersection(list1, list2); // ["b"]

集合差集

List<String> list1 = Arrays.asList("a", "b");
List<String> list2 = Arrays.asList("b", "c");
List<String> disjunctionList = (List<String>)CollectionUtil.disjunction(list1, list2); // ["a", "c"]

集合是否包含元素

List<String> list = Arrays.asList("a", "b", "c");
boolean contains = CollectionUtil.contains(list, "a"); // true

List<String> list2 = Arrays.asList("a", "b", "c");
List<String> subList = Arrays.asList("a", "b");
boolean containsAll = CollectionUtil.containsAll(list2, subList); // true

集合是否包含指定元素(自定义条件)

List<String> list = Arrays.asList("a", "b", "c");
boolean contains = CollectionUtil.contains(list, e -> e.equals("a")); // true

集合分页

List<String> list = Arrays.asList("a", "b", "c", "d", "e");
List<String> pageList = CollectionUtil.page(1, 2,list); // ["c", "d"],页码索引从0开始

集合分组

List<User> list = Arrays.asList(
    new User("张三", 18, 1),
    new User("李四", 18, 2),
    new User("王五", 18, 3),
    new User("赵六", 18, 4),
    new User("老七", 18, 1),
    new User("勾八", 18, 1),
    new User("孙九", 18, 3),
    new User("郑十", 18, 2),
    new User("root", 18, 2));
List<List<User>> groupedList = Co
System.out.println(groupedList);
//[[User(name=张三, age=18, grade=1), User(name=老七, age=18, grade=1), User(name=勾八, age=18, grade=1)], 
//[User(name=李四, age=18, grade=2), User(name=郑十, age=18, grade=2), User(name=root, age=18, grade=2)], 
//[User(name=王五, age=18, grade=3), User(name=孙九, age=18, grade=3)], 
//[User(name=赵六, age=18, grade=4)]]

集合转字符串

List<String> list = Arrays.asList("a", "b", "c");
String joinedString = CollectionUtil.join(list, ","); // "a,b,c"

元素添加

List<String> list = new ArrayList<>(Arrays.asList("1", "2"
CollectionUtil.addAll(list,"4");
CollectionUtil.addAll(list,new String[]{"5","6"});
CollectionUtil.addAll(list,Arrays.asList("7","8"));
CollectionUtil.addAll(list,new HashSet<>(Arrays.asList("9"
System.out.println(list);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3"));
CollectionUtil.addAllIfNotContains(list, Arrays.asList("1", "2", "4"));
System.out.println(list);//[1, 2, 3, 4]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3"));
CollectionUtil.addIfAbsent(list,"2");//已存在不添加
CollectionUtil.addIfAbsent(list,"4");
System.out.println(list);//[1, 2, 3, 4]

元素删除

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","","4",null,"5"));
CollectionUtil.removeEmpty(list);
//CollectionUtil.removeBlank(list);
System.out.println(list);//[1, 2, 3, 4,5]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","4","3","5","3","2"));
CollectionUtil.removeAny(list, "3","2");
System.out.println(list);//[1, 4,5]

List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","","4",null,"5"));
CollectionUtil.removeNull(list);
System.out.println(list);//[1, 2, 3, 4,5]

//自定义条件
List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3","","4",null,"5"));
CollectionUtil.removeWithAddIf(list, item -> item == null || item.isEmpty());
System.out.println(list);//[1, 2, 3, 4,5]

根据属性转Map

List<User> list = Arrays.asList(
    new User(1,"张三", 18, 1),
    new User(2,"李四", 18, 2),
    new User(3,"王五", 18, 3),
    new User(4,"张三", 18, 4),
    new User(5,"李四", 18, 1));
Map<String, User> result = CollectionUtil.fieldValueMap(list, "id");

Map<String, User> result2 = CollectionUtil.fieldValueAsMap(list, "id", "name");

获取元素

List<User> list = Arrays.asList(
            new User(1,"张三", 18, 1),
            new User(2,"李四", 18, 2),
            new User(3,"王五", 18, 3),
            new User(4,"张三", 18, 4),
            new User(5,"李四", 18, 1));
User result = CollectionUtil.get(list,6);//自带判空效果
User result = CollectionUtil.getFirst(list);
User result = CollectionUtil.getLast(list);
List<User> result = CollectionUtil.getAny(list, 0,  2);
List<Object> result = CollectionUtil.getFieldValues(list, "name");//[张三, 李四, 王五, 张三, 李四]  User类要先实现Iterable<E>接口

//截取集合
List<User> result = CollectionUtil.sub(list, 0, 3);

获取最大/最小元素

List<User> list = Arrays.asList(
    new User(1,"张三", 19, 1),
    new User(2,"李四", 18, 2),
    new User(3,"王五", 16, 3),
    new User(4,"张三", 24, 4),
    new User(5,"李四", 17, 1));
//需要User先实现Comparable<e>接口
User result = CollectionUtil.max(list);//张三
User result = CollectionUtil.min(list);//王五

集合排序

List<User> list = Arrays.asList(
    new User(1,"张三", 19, 1),
    new User(2,"李四", 18, 2),
    new User(3,"王五", 16, 3),
    new User(4,"张三", 24, 4),
    new User(5,"李四", 17, 1));
//不需要User先实现Comparable<e>接口,默认从小到大排序
List<User> result = CollectionUtil.sort(list, Comparator.comparing(User::getAge));

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2384615.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Oracle 11g导出数据库结构和数据

第一种方法&#xff1a;Plsql 利用plsql可视化工具导出&#xff0c;首先根据步骤导出表结构&#xff1a; 工具(Tools)->导出用户对象(export user objects)。 其次导出数据表结构&#xff1a; 工具(Tools)->导出表(export Tables)->选中表->sql inserts(where语…

零基础设计模式——创建型模式 - 抽象工厂模式

第二部分&#xff1a;创建型模式 - 抽象工厂模式 (Abstract Factory Pattern) 我们已经学习了单例模式&#xff08;保证唯一实例&#xff09;和工厂方法模式&#xff08;延迟创建到子类&#xff09;。现在&#xff0c;我们来探讨创建型模式中更为复杂和强大的一个——抽象工厂…

解锁内心的冲突:神经症冲突的理解与解决之道

目录 一、神经症冲突概述 二、冲突的基本类型 三、未解决冲突的后果 四、尝试解决的途径 五、真正解决冲突 六、总结 干货分享&#xff0c;感谢您的阅读&#xff01; 人类的内心世界复杂多变&#xff0c;常常充满了各种冲突和矛盾。每个人在成长的过程中&#xff0c;都或…

Redisson读写锁和分布式锁的项目实践

解决方案:采用读写锁 什么是读写锁 Redisson读写锁是一种基于Redis实现特殊的机制,用于在分布式系统中协调对共享资源的访问,其继承了Java中的ReentrantReadWriteLock的思想.特别适用于读多写少的场景.其核心是:允许多个线程同时读取共享资源,但写操作必须占用资源.从而保证线…

SkyWalking高频采集泄漏线程导致CPU满载排查思路

SkyWalking高频采集泄漏线程导致CPU满载排查思路 契机 最近在消除线上服务告警&#xff0c;发现Java线上测试服经常CPU满载告警&#xff0c;以前都是重启解决&#xff0c;今天好好研究下&#xff0c;打arthas火焰图发现是SkyWalking-agent的线程采集任务一直在吃cpu&#xff…

【HarmonyOS 5】Map Kit 地图服务之应用内地图加载

#HarmonyOS SDK应用服务&#xff0c;#Map Kit&#xff0c;#应用内地图 目录 前期准备 AGC 平台创建项目并创建APP ID 生成调试证书 生成应用证书 p12 与签名文件 csr 获取 cer 数字证书文件 获取 p7b 证书文件 配置项目签名 配置签名证书指纹 项目开发 配置Client I…

ld: cpu type/subtype in slice (arm64e.old) does not match fat header (arm64e)

ld: cpu type/subtype in slice (arm64e.old) does not match fat header (arm64e) in ‘/Users/*****/MposApp/MposApp/Modules/Common/Mpos/NewLand/MESDK.framework/MESDK’ clang: error: linker command failed with exit code 1 (use -v to see invocation) 报错 解决方…

通过vue-pdf和print-js实现PDF和图片在线预览

npm install vue-pdf npm install print-js <template><div><!-- PDF 预览模态框 --><a-modal:visible"showDialog":footer"null"cancel"handleCancel":width"800":maskClosable"true":keyboard"…

视频监控管理平台EasyCVR结合AI分析技术构建高空抛物智能监控系统,筑牢社区安全防护网

高空抛物严重威胁居民生命安全与公共秩序&#xff0c;传统监管手段存在追责难、威慑弱等问题。本方案基于EasyCVR视频监控与AI视频分析技术&#xff08;智能分析网关&#xff09;&#xff0c;构建高空抛物智能监控系统&#xff0c;实现24小时实时监测、智能识别与精准预警&…

2.2.1 05年T1复习

引言 从现在进去考研英语基础阶段的进阶&#xff0c;主要任务还是05-09年阅读真题的解题&#xff0c;在本阶段需要注意正确率。阅读最后目标&#xff1a;32-34分&#xff0c;也就是每年真题最多错四个。 做题步骤&#xff1a; 1. 预习&#xff1a;读题干并找关键词 做题&#…

Python-11(集合)

与字典类似&#xff0c;集合最大的特点就是唯一性。集合中所有的元素都应该是独一无二的&#xff0c;并且也是无序的。 创建集合 使用花括号 set {"python","Java"} print(type(set)) 使用集合推导式 set {s for s in "python"} print(set…

Opixs: Fluxim推出的全新显示仿真模拟软件

Opixs 是 Fluxim 最新研发的显示仿真模拟软件&#xff0c;旨在应对当今显示技术日益复杂的挑战。通过 Opixs&#xff0c;研究人员和工程师可以在制造前&#xff0c;设计并验证 新的像素架构&#xff0c;从而找出更功节能、色彩表现更优的布局方案。 Opixs 适用于学术研究和工业…

佰力博与您探讨PVDF薄膜极化特性及其影响因素

PVDF&#xff08;聚偏氟乙烯&#xff09;薄膜的极化是其压电性能形成的关键步骤&#xff0c;通过极化处理可以显著提高其压电系数和储能能力。极化过程涉及多种方法和条件&#xff0c;以下从不同角度详细说明PVDF薄膜的极化特性及其影响因素。 1、极化方法 热极化&#xff1a;…

自动获取ip地址安全吗?如何自动获取ip地址

在数字化网络环境中&#xff0c;IP地址的获取方式直接影响设备连接的便捷性与安全性。自动获取IP地址&#xff08;通过DHCP协议&#xff09;虽简化了配置流程&#xff0c;但其安全性常引发用户疑虑。那么&#xff0c;自动获取IP地址安全吗&#xff1f;如何自动获取IP地址&#…

STM32:深度解析RS-485总线与SP3485芯片

32个设备 知识点1【RS-485的简介】 RS-485是一种物理层差分总线标准&#xff0c;在串口的基础上演变而来&#xff1b; 两者虽然不在同一层次上直接对等&#xff0c;但在实际系统中&#xff0c;往往使用RS-485驱动差分总线&#xff0c;将USART转换为适合长距离、多点通信的物…

亚马逊搜索代理: 终极指南

文章目录 前言一、为什么需要代理来搜索亚马逊二、如何选择正确的代理三、搜索亚马逊的最佳代理类型四、为亚马逊搜索设置代理五、常见挑战及克服方法六、亚马逊搜索的替代方法总结 前言 在没有代理的情况下搜索亚马逊会导致 IP 禁止、验证码和速度限制&#xff0c;从而使数据…

C++笔记-封装红黑树实现set和map

1.源码及框架分析 上面就是在stl库中set和map的部分源代码。 通过上图对框架的分析&#xff0c;我们可以看到源码中rb_tree⽤了⼀个巧妙的泛型思想实现&#xff0c;rb_tree是实 现key的搜索场景&#xff0c;还是key/value的搜索场景不是直接写死的&#xff0c;⽽是由第⼆个模板…

留给王小川的时间不多了

王小川&#xff0c;这位头顶“天才少年”光环的清华学霸、搜狗输入法创始人、中国互联网初代技术偶像&#xff0c;正迎来人生中最难啃的硬骨头。 他在2023年创立的百川智能&#xff0c;被称为“大模型六小虎”之一。今年4月&#xff0c;王小川在全员信中罕见地反思过去两年工作…

国产频谱仪性能如何?矢量信号分析仪到底怎么样?

矢量信号分析仪是一种高性能的电子测量设备&#xff0c;具备频谱分析、矢量信号分析、实时频谱分析、脉冲信号分析、噪声系数测量、相位噪声测量等多种功能。它能够对各类复杂信号进行精确的频谱特性分析、调制质量评估、信号完整性检测以及干扰源定位等操作。广泛应用于通信、…

熔断器(Hystrix,Resilience4j)

熔断器 核心原理​ 熔断器通过监控服务调用失败率&#xff0c;在达到阈值时自动切断请求&#xff0c;进入熔断状态&#xff08;类似电路保险丝&#xff09;。其核心流程为&#xff1a; 关闭状态&#xff08;Closed&#xff09;​​&#xff1a;正常处理请求&#xff0c;统计失…