CollectionUtils

news/2024/7/6 13:39:01 标签: java

使用 CollectionUtils 类的常用方法

在Java开发中,我们经常需要对集合进行各种操作,而Apache Commons Collections库提供了一个方便的工具类 CollectionUtils,其中包含了许多实用的方法。在这篇博客中,我们将深入了解一些常用的方法,并提供详细的示例。

1. isEmpty

java">// Check if a list is empty
List<String> emptyList = new ArrayList<>();
boolean isEmpty = CollectionUtils.isEmpty(emptyList);
System.out.println("Is the list empty? " + isEmpty);//Is the list empty? true

这个方法用于检查给定的集合是否为空。在上面的例子中,我们创建了一个空的 ArrayList,然后使用 isEmpty 方法检查它是否为空,最后打印结果。

2. isNotEmpty

java">// Check if a list is not empty
List<String> nonEmptyList = Arrays.asList("item1", "item2");
boolean isNotEmpty = CollectionUtils.isNotEmpty(nonEmptyList);
System.out.println("Is the list not empty? " + isNotEmpty);//Is the list empty? true

isEmpty 相反,isNotEmpty 方法用于检查集合是否不为空。我们创建了一个包含一些元素的列表,并使用 isNotEmpty 方法进行检查。

isEmptyisNotEmpty

  • 时间复杂度: O(1) - 常数时间。这是因为这两个方法只需检查集合是否为 null 或其大小是否为零。
  • 适用场景: 用于快速检查集合是否为空。

3. size

java">// Get the size of a list
int size = CollectionUtils.size(nonEmptyList);
System.out.println("Size of the list: " + size);//Size of the list: 2

size 方法返回给定集合的大小。在这个例子中,我们获取了之前非空列表的大小并打印输出。

size

  • 时间复杂度: O(1) - 常数时间。这是因为 size 方法通常由集合实现提供,直接返回已存储的大小。
  • 适用场景: 适用于快速获取集合的大小。

4. addAll

java">// Merge two lists
List<String> destinationList = new ArrayList<>();
CollectionUtils.addAll(destinationList, "item3", "item4");
System.out.println("Merged list: " + destinationList);//Merged list: [item3, item4]

addAll 方法用于将一个集合的所有元素添加到另一个集合中。在这里,我们创建了一个目标列表,并使用 addAll 将两个元素添加到这个列表中。

addAll

  • 时间复杂度: O(n) - 线性时间,其中 n 是要添加的元素数量。
  • 适用场景: 用于将一个集合的所有元素添加到另一个集合。在元素数量较大时可能会影响性能。

5. removeAll

java">// Remove elements from a list
CollectionUtils.removeAll(destinationList, "item3");
System.out.println("List after removal: " + destinationList);//List after removal: []

removeAll 方法用于从集合中移除指定的元素。在这个例子中,我们从目标列表中移除了一个元素,并打印输出修改后的列表。

removeAll

  • 时间复杂度: O(n) - 线性时间,其中 n 是要移除的元素数量。
  • 适用场景: 用于从集合中移除指定的元素。

6. intersection

java">// Get the intersection of two lists
List<String> list1 = Arrays.asList("apple", "orange", "banana");
List<String> list2 = Arrays.asList("banana", "kiwi", "apple");
List<String> intersection = (List<String>) CollectionUtils.intersection(list1, list2);
System.out.println("Intersection of lists: " + intersection);//Intersection of lists: [banana, apple]

intersection 方法返回两个集合的交集。在这里,我们创建了两个包含水果的列表,并使用 intersection 方法获取它们的交集。

7. union

java">// Get the union of two lists
List<String> union = (List<String>) CollectionUtils.union(list1, list2);
System.out.println("Union of lists: " + union);//Union of lists: [apple, orange, banana, kiwi]

union 方法返回两个集合的并集。在这个例子中,我们使用 union 方法获取两个水果列表的并集。

8. disjunction

java">// Get the disjunction of two lists
List<String> disjunction = (List<String>) CollectionUtils.disjunction(list1, list2);
System.out.println("Disjunction of lists: " + disjunction);//Disjunction of lists: [orange, kiwi]

disjunction 方法返回两个集合的互斥集合,即不属于交集的部分。在这里,我们使用 disjunction 方法获取两个列表的互斥部分。

9. subtract

java">// Subtract one list from another
CollectionUtils.subtract(list1, list2);
System.out.println("List1 after subtracting list2: " + list1);//List1 after subtracting list2: [orange]

subtract 方法用于从第一个集合中移除第二个集合中包含的元素。在这个例子中,我们使用 subtract 方法从 list1 中移除了与 list2 重叠的元素。

subtract

  • 时间复杂度: O(m + n) - 线性时间,其中 m 和 n 分别是两个集合的大小。
  • 适用场景: 用于从一个集合中移除另一个集合包含的元素。

10. filter

java">// Filter elements based on a condition
List<String> filteredList = (List<String>) CollectionUtils.select(list1, s -> s.startsWith("a"));
System.out.println("Filtered list: " + filteredList);//Filtered list: [apple]

filter 方法根据给定的条件保留集合中的元素。在这里,我们使用 filter 方法保留了以字母 “a” 开头的元素。

filter

  • 时间复杂度: O(n) - 线性时间,其中 n 是集合的大小。
  • 适用场景: 用于根据条件筛选集合中的元素。

11. transform

java">// Transform elements in a list
List<Integer> lengths = (List<Integer>) CollectionUtils.collect(list1, String::length);
System.out.println("Lengths of items in the list: " + lengths);//Lengths of items in the list: [5, 6, 6]

transform 方法用于对集合中的元素进行转换。在这个例子中,我们使用 transform 方法获取了 list1 中每个字符串元素的长度,并将结果存储在 lengths 列表中。

transform

  • 时间复杂度: O(n) - 线性时间,其中 n 是集合的大小。
  • 适用场景: 用于对集合中的元素进行转换。

12. countMatches

java">// Count elements that match a condition
long count = CollectionUtils.countMatches(list1, s -> s.length() > 5);
System.out.println("Number of items with length > 5: " + count);//Number of items with length > 5: 2

countMatches 方法用于计算满足特定条件的元素数量。在这个例子中,我们计算了 list1 中长度大于 5 的元素的数量。

countMatches

  • 时间复杂度: O(n) - 线性时间,其中 n 是集合的大小。
  • 适用场景: 用于计算满足特定条件的元素数量。

http://www.niftyadmin.cn/n/5292612.html

相关文章

【快速全面掌握 WAMPServer】07.整明白 MySQL 和 MariaDB

网管小贾 / sysadm.cc 上一篇我们学习了 PHP &#xff0c;今天我们接着来学习了解一下另一位 LAMP 中的 M &#xff0c;同样是作为四剑客之一的 MySQL/MariaDB 。 MySQL/MariaDB 实际上是指数据库&#xff0c;是一种关系型数据库管理系统&#xff0c;可与 PHP 解释器一起配合&…

win10安装ffmpeg

1 ffmpeg官网下载 官网地址&#xff1a;https://ffmpeg.org/ ffmpeg可执行程序下载地址&#xff1a;https://www.gyan.dev/ffmpeg/builds/ ffmpeg官网文档&#xff1a;https://ffmpeg.org/documentation.html 选择对应的版本点解下载可执行程序包&#xff0c;比如6.1版本的…

2024年第三届服务机器人国际会议(ICoSR 2024) | Ei、Scopus双检索

会议简介 Brief Introduction 2024年第三届服务机器人国际会议(ICoSR 2024) 会议时间&#xff1a;2024年7月26日-28日 召开地点&#xff1a;中国杭州 大会官网&#xff1a;www.iwosr.org 进入新时代&#xff0c;科技更新迭代快速发展&#xff0c;机器人不仅变得更加节能&#x…

Python 爬虫 教程

python爬虫框架&#xff1a;Scrapyd&#xff0c;Feapder&#xff0c;Gerapy 参考文章&#xff1a; python爬虫工程师&#xff0c;如何从零开始部署ScrapydFeapderGerapy&#xff1f; - 知乎 神器&#xff01;五分钟完成大型爬虫项目 - 知乎 爬虫框架-feapder - 知乎 scrap…

网络安全设备-学习笔记01(防火墙、waf、ids、蜜罐)

网络安全设备-学习笔记01 参考来源&#xff1a; 大佬的课件笔记&#xff1a;安全攻防技能38讲 防火墙 防火墙类型 根据实现方式和功能的不同&#xff0c;分为三种类型&#xff1a;包过滤防火墙、应用网关防火墙和状态检测防火墙 包过滤防火墙 工作在网络层和传输层&#…

用ChatGPT挑选钻石!著名珠宝商推出-珠宝GPT

根据Salesforce最新发布的第五版《互联网购物报告》显示&#xff0c;ChatGPT等生成式AI的出现、快速发展&#xff0c;对零售行业和购物者产生了较大影响。可有效简化业务流程实现降本增效&#xff0c;并改善购物体验。 著名珠宝商James Allen为了积极拥抱生成式AI全面提升销售…

FFmpeg Filter

原理 1.将压缩后的每一帧数据进行解码 2.对解码后的数据进行计算 3.再将处理好的数据进行编码 简单滤镜 ffplay -i /Users/king/Desktop/ffmpeg/audio/cut.mp4 -vf "drawboxx30:y30:w60:h60:cred" drawbox 滤镜名字 后边用等号连接参数&#xff0c;参数使用冒…

java 程序堵塞的排查方式

java 程序堵塞的排查方式 当java 程序堵塞 任何异常 信息&#xff0c;该如何排查。## 死锁问题先确实是否死锁问题&#xff0c;使用arthas &#xff0c;执行 thread -b 命令。不是死锁问题&#xff0c;执行 jstack 命令 保留 当前执行命令。## 分析Jstack未知全貌 不予置评打完…