工具类学习-CollectionUtils

2024-03-03 17:48

本文主要是介绍工具类学习-CollectionUtils,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

CollectionUtils是日常经常会用到的一个工具类,在包org.apache.commons.collections中。

目前最常用到的两个方法是CollectionUtils.isEmpty()以及CollectionUtils.isNotEmpty()。

还有待补充一些使用实例

 

其中有一个私有的静态变量INTEGER_ONE:

private static Integer INTEGER_ONE = new Integer(1);

 

一个不可修改的空集合:

public static final Collection EMPTY_COLLECTION = UnmodifiableCollection.decorate(new ArrayList());

 

取并集方法:

public static Collection union(Collection a, Collection b) {ArrayList list = new ArrayList();Map mapa = getCardinalityMap(a);// 该方法以Collection中的元素作为key,出现次数作为value。Map mapb = getCardinalityMap(b);Set elts = new HashSet(a);elts.addAll(b);Iterator it = elts.iterator();while(it.hasNext()) {Object obj = it.next();int i = 0;for(int m = Math.max(getFreq(obj, mapa), getFreq(obj, mapb)); i < m; ++i)             {// getFrep是从指定map中获取该元素出现的次数list.add(obj);}}return list;}

使用方法:

List<Integer> list1 = Lists.newArrayList(1, 3, 5, 7, 9, 9, 9);List<Integer> list2 = Lists.newArrayList(1, 1, 2, 4, 5, 7, 9, 9, 9, 9);Collection<Integer> result = CollectionUtils.union(list1, list2);for (Integer i : result) {System.out.print(i + " ");}

输出如下:

1 1 2 3 4 5 7 9 9 9 9 

 

取交集:

public static Collection intersection(final Collection a, final Collection b) {ArrayList list = new ArrayList();Map mapa = getCardinalityMap(a);Map mapb = getCardinalityMap(b);Set elts = new HashSet(a);elts.addAll(b);Iterator it = elts.iterator();while(it.hasNext()) {Object obj = it.next();for(int i=0,m=Math.min(getFreq(obj,mapa),getFreq(obj,mapb));i<m;i++) {list.add(obj);}}return list;}

用法:

List<Integer> list1 = Lists.newArrayList(1, 3, 5, 7, 9, 9, 9);List<Integer> list2 = Lists.newArrayList(1, 1, 2, 4, 5, 7, 9, 9, 9, 9);Collection<Integer> result = CollectionUtils.intersection(list1, list2);for (Integer i : result) {System.out.print(i + " ");}

输出:

1 5 7 9 9 9 

 

取得交集之外的部分:

public static Collection disjunction(final Collection a, final Collection b) {ArrayList list = new ArrayList();Map mapa = getCardinalityMap(a);Map mapb = getCardinalityMap(b);Set elts = new HashSet(a);elts.addAll(b);Iterator it = elts.iterator();while(it.hasNext()) {Object obj = it.next();for(int i=0,m=((Math.max(getFreq(obj,mapa),getFreq(obj,mapb)))-(Math.min(getFreq(obj,mapa),getFreq(obj,mapb))));i<m;i++) {list.add(obj);}}return list;}

用法:

List<Integer> list1 = Lists.newArrayList(1, 3, 5, 7, 9, 9, 9);List<Integer> list2 = Lists.newArrayList(1, 1, 2, 4, 5, 7, 9, 9, 9, 9);Collection<Integer> result = CollectionUtils.disjunction(list1, list2);for (Integer i : result) {System.out.print(i + " ");}

 

输出:

1 2 3 4 9 

 

从一个集合中移除另一个集合中存在的元素:

public static Collection subtract(final Collection a, final Collection b) {ArrayList list = new ArrayList( a );for (Iterator it = b.iterator(); it.hasNext();) {list.remove(it.next());}return list;}

 

判断两个集合是否存在交集:

public static boolean containsAny(final Collection coll1, final Collection coll2) {if (coll1.size() < coll2.size()) {for (Iterator it = coll1.iterator(); it.hasNext();) {if (coll2.contains(it.next())) {return true;}}} else {for (Iterator it = coll2.iterator(); it.hasNext();) {if (coll1.contains(it.next())) {return true;}}}return false;}

是否是子集:

public static boolean isSubCollection(final Collection a, final Collection b) {Map mapa = getCardinalityMap(a);Map mapb = getCardinalityMap(b);Iterator it = a.iterator();while (it.hasNext()) {Object obj = it.next();if (getFreq(obj, mapa) > getFreq(obj, mapb)) {return false;}}return true;}

是否是真子集:

public static boolean isProperSubCollection(final Collection a, final Collection b) {return (a.size() < b.size()) && CollectionUtils.isSubCollection(a,b);}

 

判断两个集合是否相等:

public static boolean isEqualCollection(final Collection a, final Collection b) {if(a.size() != b.size()) {return false;} else {Map mapa = getCardinalityMap(a);Map mapb = getCardinalityMap(b);if(mapa.size() != mapb.size()) {return false;} else {Iterator it = mapa.keySet().iterator();while(it.hasNext()) {Object obj = it.next();if(getFreq(obj,mapa) != getFreq(obj,mapb)) {return false;}}return true;}}}

对象在集合中出现的次数:

public static int cardinality(Object obj, final Collection coll) {if (coll instanceof Set) {return (coll.contains(obj) ? 1 : 0);}if (coll instanceof Bag) {return ((Bag) coll).getCount(obj);}int count = 0;if (obj == null) {for (Iterator it = coll.iterator();it.hasNext();) {if (it.next() == null) {count++;}}} else {for (Iterator it = coll.iterator();it.hasNext();) {if (obj.equals(it.next())) {count++;}}}return count;}

找到集合中第一个满足条件的元素:

public static Object find(Collection collection, Predicate predicate) {if (collection != null && predicate != null) {for (Iterator iter = collection.iterator(); iter.hasNext();) {Object item = iter.next();if (predicate.evaluate(item)) {return item;}}}return null;}

对集合中每个元素执行指定的闭包:

public static void forAllDo(Collection collection, Closure closure) {if (collection != null && closure != null) {for (Iterator it = collection.iterator(); it.hasNext();) {closure.execute(it.next());}}}

过滤掉某些不满足指定条件的元素:

public static void filter(Collection collection, Predicate predicate) {if (collection != null && predicate != null) {for (Iterator it = collection.iterator(); it.hasNext();) {if (predicate.evaluate(it.next()) == false) {it.remove();}}}}

对集合中每个元素进行指定的操作,这个操作会影响传入的集合:

public static void transform(Collection collection, Transformer transformer) {if (collection != null && transformer != null) {if (collection instanceof List) {List list = (List) collection;for (ListIterator it = list.listIterator(); it.hasNext();) {it.set(transformer.transform(it.next()));}} else {Collection resultCollection = collect(collection, transformer);collection.clear();collection.addAll(resultCollection);}}}

返回满足指定条件的元素个数:

public static int countMatches(Collection inputCollection, Predicate predicate) {int count = 0;if (inputCollection != null && predicate != null) {for (Iterator it = inputCollection.iterator(); it.hasNext();) {if (predicate.evaluate(it.next())) {count++;}}}return count;}

判断集合中是否存在满足某个指定条件的元素:

public static boolean exists(Collection collection, Predicate predicate) {if (collection != null && predicate != null) {for (Iterator it = collection.iterator(); it.hasNext();) {if (predicate.evaluate(it.next())) {return true;}}}return false;}

找出集合中满足指定条件的所有元素:

public static Collection select(Collection inputCollection, Predicate predicate) {ArrayList answer = new ArrayList(inputCollection.size());select(inputCollection, predicate, answer);return answer;}

找出集合中所有不满足条件的元素:

public static Collection selectRejected(Collection inputCollection, Predicate predicate) {ArrayList answer = new ArrayList(inputCollection.size());selectRejected(inputCollection, predicate, answer);return answer;}

 

添加一个可能为空的元素:

public static boolean addIgnoreNull(Collection collection, Object object) {return (object == null ? false : collection.add(object));}

向集合中添加一个迭代器、枚举、数组:

public static void addAll(Collection collection, Iterator iterator) {while (iterator.hasNext()) {collection.add(iterator.next());}}

 

其余还有获取第几个元素的index()方法、获取集合大小的size()方法、获取集合为空、不为空、反转数组reverseArray()、集合是否已经满的isFull()方法、保留全部retainAll()、移除全部removeAll()、获取同步集合、不可修改集合等方法。

 

这篇关于工具类学习-CollectionUtils的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/770426

相关文章

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

使用Java实现通用树形结构构建工具类

《使用Java实现通用树形结构构建工具类》这篇文章主要为大家详细介绍了如何使用Java实现通用树形结构构建工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录完整代码一、设计思想与核心功能二、核心实现原理1. 数据结构准备阶段2. 循环依赖检测算法3. 树形结构构建4. 搜索子

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

jvm调优常用命令行工具详解

《jvm调优常用命令行工具详解》:本文主要介绍jvm调优常用命令行工具的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一 jinfo命令查看参数1.1 查看jvm参数二 jstack命令2.1 查看现场堆栈信息三 jstat 实时查看堆内存,gc情况3.1

MySQL使用binlog2sql工具实现在线恢复数据功能

《MySQL使用binlog2sql工具实现在线恢复数据功能》binlog2sql是大众点评开源的一款用于解析MySQLbinlog的工具,根据不同选项,可以得到原始SQL、回滚SQL等,下面我们就来... 目录背景目标步骤准备工作恢复数据结果验证结论背景生产数据库执行 SQL 脚本,一般会经过正规的审批

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)

《Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)》:本文主要介绍Java导入、导出excel的相关资料,讲解了使用Java和ApachePOI库将数据导出为Excel文件,包括... 目录前言一、引入Apache POI依赖二、用法&步骤2.1 创建Excel的元素2.3 样式和字体2.

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图