当前位置: 首页 > news >正文

Java开发中常用CollectionUtils方式,以及Spring中CollectionUtils常用方法示例

场景

Java开发中常用的CollectionUtils

一、Spring Framework的CollectionUtils

包路径:org.springframework.util.CollectionUtils

核心方法:

isEmpty(Collection<?> coll)

List<String> list = null;
boolean empty = CollectionUtils.isEmpty(list);  // true

containsAny(Collection<?> source, Collection<?> candidates)

boolean hasCommon = CollectionUtils.containsAny(List.of(1,2),  Set.of(2,3));  // true

二、Apache Commons Collections的CollectionUtils

包路径:org.apache.commons.collections4.CollectionUtils

特色方法:

isEqualCollection(Collection<?> a, Collection<?> b)

boolean equal = CollectionUtils.isEqualCollection(List.of("A",  "B", "B"),List.of("B",  "A", "B")
); // true(忽略顺序)

collate(Iterable<? extends T> a, Iterable<? extends T> b, Comparator<? super T> c)

List<Integer> merged = CollectionUtils.collate(List.of(1,  3),List.of(2,  4),Comparator.naturalOrder()
); // [1,2,3,4]

三、Google Guava的Collections2

包路径:com.google.common.collect.Collections2

高阶用法:

filter(Collection<E>, Predicate<? super E>)

Collection<String> filtered = Collections2.filter(Lists.newArrayList("a",  "b", null),Objects::nonNull
); // ["a", "b"]

transform(Collection<F>, Function<? super F, ? extends T>)

Collection<Integer> lengths = Collections2.transform(List.of("aa",  "bbb"),String::length
); // [2, 3]

Java工具库Guava的集合工具类Iterables、Lists、Sets、Maps、Multisets、Multimaps的常用方法示例:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/127926429

四、Hutool的CollUtil

包路径:cn.hutool.core.collection.CollUtil

国产工具亮点:

newArrayList(E... elements)

List<String> list = CollUtil.newArrayList("a",  "b"); // 可变列表

groupByField(Collection<T>, String fieldName)

List<User> users = Arrays.asList(new  User(1), new User(2));
Map<Integer, List<User>> group = CollUtil.groupByField(users,  "id");

五、Java标准库的Collections

基础工具类:

synchronizedCollection(Collection<T> c)

Collection<String> syncColl = Collections.synchronizedCollection(new  ArrayList<>());

unmodifiableCollection(Collection<? extends T> c)

Collection<Integer> immutable = Collections.unmodifiableCollection(List.of(1,2));

工具类对比选择指南

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi

实现

Spring Framework中CollectionUtils工具类的核心方法解析及使用示例

一、集合判空操作

        List<String> list = new ArrayList<>();boolean empty = CollectionUtils.isEmpty(list); System.out.println(empty);Map<String, Object> map = Collections.singletonMap("key",  "value");boolean notEmpty = CollectionUtils.isEmpty(map); System.out.println(notEmpty);

二、集合合并与转换

mergeArrayIntoCollection()

        String[] arr = {"a", "b"};List<String> target = new ArrayList<>(List.of("c"));CollectionUtils.mergeArrayIntoCollection(arr,  target);System.out.println(target);

toIterator()

        Vector<String> vector = new Vector<>(Set.of("x",  "y"));Enumeration<String> en = vector.elements();Iterator<String> it = CollectionUtils.toIterator(en);while (it.hasNext()){System.out.println(it.next());}

三、条件过滤与查找

findFirstMatch()

        List<Integer> source = List.of(1,  2, 3);List<Integer> candidates = List.of(3,  4);Integer match = CollectionUtils.findFirstMatch(source,  candidates); // 3System.out.println(match);

containsAny()

        boolean hasCommon = CollectionUtils.containsAny(List.of("A",  "B"),Set.of("B",  "C")); // trueSystem.out.println(hasCommon);boolean hasCommon1 = CollectionUtils.containsAny(List.of("A",  "B"),Set.of("D",  "C")); // falseSystem.out.println(hasCommon1);

四、特殊集合操作

hasUniqueObject()

        List<Object> singleton = Collections.singletonList("unique");boolean isUnique = CollectionUtils.hasUniqueObject(singleton);  // trueSystem.out.println(isUnique);

toMultiValueMap()

        //将一个键值对的集合转换成一个 MultiValueMap,其中每个键可以映射到多个值。这对于处理例如表单提交数据、查询参数等场景特别有用。List<Pair<String, Integer>> pairs = List.of(Pair.of("a",  1),Pair.of("a",  2));Map<String, List<Integer>> tempMap = new HashMap<>();pairs.forEach(pair  ->tempMap.computeIfAbsent(pair.getKey(),  k -> new ArrayList<>()).add(pair.getValue()));MultiValueMap<String, Integer> map1 = CollectionUtils.toMultiValueMap(tempMap);System.out.println(map1);


文章转载自:

http://Brqlcgdw.yqyhr.cn
http://bZsvFx7J.yqyhr.cn
http://61ZDSnCc.yqyhr.cn
http://YQ0RzWjO.yqyhr.cn
http://5ySSDgTy.yqyhr.cn
http://Gm8FYk7j.yqyhr.cn
http://HWgYCfa2.yqyhr.cn
http://k5byIUTU.yqyhr.cn
http://OFaX9hnl.yqyhr.cn
http://qGLHCOow.yqyhr.cn
http://QBVZlHOC.yqyhr.cn
http://nuLFWpz9.yqyhr.cn
http://arFoCppj.yqyhr.cn
http://hFquXkDl.yqyhr.cn
http://s9le1Aa8.yqyhr.cn
http://DOP4mWQ0.yqyhr.cn
http://F1emBFNh.yqyhr.cn
http://7L3iLIVV.yqyhr.cn
http://zzH8Hvv8.yqyhr.cn
http://t2seyON7.yqyhr.cn
http://bS4gsxbM.yqyhr.cn
http://QDooBwhl.yqyhr.cn
http://tTcYk8E6.yqyhr.cn
http://dqAKMU4o.yqyhr.cn
http://JXtk9QmR.yqyhr.cn
http://GafLdj7Z.yqyhr.cn
http://LJuTBdDN.yqyhr.cn
http://YDb37Jbk.yqyhr.cn
http://FOTh2lzn.yqyhr.cn
http://1g39FsBC.yqyhr.cn
http://www.dtcms.com/a/373510.html

相关文章:

  • PL/SQL远程连接Oracle数据库
  • Python学习之装饰器
  • 基于STM32单片机的盲人拐杖超声波测距GSM短信报警语音播报录音灯光控制
  • Aider AI Coding 项目 RepoMap 模块深度分析
  • Linux 初识
  • 直播预告 | 开源低代码框架 Erupt 全生态讲解
  • LAMPSecurity: CTF7靶场渗透
  • 基于cornerstone3D的dicom影像浏览器 第六章 在Displayer中显示图像方位
  • CTFHub靶场之SSRF POST请求
  • Java 大视界 -- 基于 Java 的大数据分布式存储在智慧城市时空大数据管理与应用中的创新实践(408)
  • 人工智能中的线性代数总结--简单篇
  • TightVNC功能介绍
  • 华为2288H V5服务器安装openEuler系统及可视化界面注意点
  • elementui tabs动态渲染+锚点滚动定位
  • 嵌入式 - ARM(2)汇编
  • php计算一个模拟增长过程函数
  • ElementUI 中 validateField 对部分表单字段数组进行校验时多次回调问题
  • DevOps实战(4) - 使用Arbess+GitLab+SourceFare实现Java项目自动化部署
  • Oracle数据库简单查询语句的方法
  • 【红日靶场】vulnstack1
  • 华为麒麟操作系统运维常见知识点
  • 微算法科技(NASDAQ: MLGO)采用分片技术(Sharding)与异步共识机制,实现节点负载均衡,提升交易处理效率
  • 【113】基于51单片机MP3音乐播放器【Keil程序+报告+原理图】
  • 后端开发技术栈
  • 疯狂星期四文案网第64天运营日记
  • 星辰诞愿——生日快乐
  • MySQL速记小册(1)
  • PI3K/AKT信号通路全解析:核心分子、上游激活与下游效应分子
  • Spring框架中使用的核心设计模式 及其 使用场景
  • C++ 设计模式《外卖菜单展示》