工具类学习-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

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

高效录音转文字:2024年四大工具精选!

在快节奏的工作生活中,能够快速将录音转换成文字是一项非常实用的能力。特别是在需要记录会议纪要、讲座内容或者是采访素材的时候,一款优秀的在线录音转文字工具能派上大用场。以下推荐几个好用的录音转文字工具! 365在线转文字 直达链接:https://www.pdf365.cn/ 365在线转文字是一款提供在线录音转文字服务的工具,它以其高效、便捷的特点受到用户的青睐。用户无需下载安装任何软件,只

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

【Linux 从基础到进阶】Ansible自动化运维工具使用

Ansible自动化运维工具使用 Ansible 是一款开源的自动化运维工具,采用无代理架构(agentless),基于 SSH 连接进行管理,具有简单易用、灵活强大、可扩展性高等特点。它广泛用于服务器管理、应用部署、配置管理等任务。本文将介绍 Ansible 的安装、基本使用方法及一些实际运维场景中的应用,旨在帮助运维人员快速上手并熟练运用 Ansible。 1. Ansible的核心概念