CollectionUtils工具类使用指南
CollectionUtils提供很多对集合的操作方法,常用的方法如下:(参考文章:http://www.open-open.com/code/view/1420470842125)
import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
public class CollectionUtilsTest {
public static void main(String[] args) {
List<Integer> a = new ArrayList<Integer>();
List<Integer> b = null;
List<Integer> c = new ArrayList<Integer>();
c.add(5);
c.add(6);
//判断集合是否为空
System.out.println(CollectionUtils.isEmpty(a)); //true
System.out.println(CollectionUtils.isEmpty(b)); //true
System.out.println(CollectionUtils.isEmpty(c)); //false //判断集合是否不为空
System.out.println(CollectionUtils.isNotEmpty(a)); //false
System.out.println(CollectionUtils.isNotEmpty(b)); //false
System.out.println(CollectionUtils.isNotEmpty(c)); //true //两个集合间的操作
List<Integer> e = new ArrayList<Integer>();
e.add(2);
e.add(1);
List<Integer> f = new ArrayList<Integer>();
f.add(1);
f.add(2);
List<Integer> g = new ArrayList<Integer>();
g.add(12);
//比较两集合值
System.out.println(CollectionUtils.isEqualCollection(e,f)); //true
System.out.println(CollectionUtils.isEqualCollection(f,g)); //false List<Integer> h = new ArrayList<Integer>();
h.add(1);
h.add(2);
h.add(3);;
List<Integer> i = new ArrayList<Integer>();
i.add(3);
i.add(3);
i.add(4);
i.add(5);
//并集
System.out.println(CollectionUtils.union(i,h)); //[1, 2, 3, 3, 4, 5]
//交集
System.out.println(CollectionUtils.intersection(i,h)); //[3]
//交集的补集
System.out.println(CollectionUtils.disjunction(i,h)); //[1, 2, 3, 4, 5]
//e与h的差
System.out.println(CollectionUtils.subtract(h,i)); //[1, 2]
System.out.println(CollectionUtils.subtract(i,h)); //[3, 4, 5]
}
}
CollectionUtils工具类使用指南的更多相关文章
- java代码之美(12)---CollectionUtils工具类
java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...
- CollectionUtils工具类
CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的,可以使代码更加简洁和安全. 使用前需导入依赖 <dependency> <gr ...
- StringUtils、CollectionUtils工具类的常用方法
唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...
- java代码(12) ---CollectionUtils工具类
CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...
- 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空
通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...
- CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2)
一.CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2) 采用的类: import org.apache.commons.collec ...
- CollectionUtils工具类的常用方法
集合判断: 例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...
- CollectionUtils工具类中常用方法
@SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...
- 集合工具类CollectionUtils、ListUtils、SetUtils、MapUtils的使用
主要用它的isEmpty(final Collection<?> coll)静态方法来判断一个给定的集合是否为null或者是否长度为0.最近才发现此工具类还可以取集合的交集.并集.甚至差集 ...
随机推荐
- 关于shared pool的深入探讨(一) 【转载】
关于shared pool的深入探讨(一) 作者:eygle |English [转载时请标明出处和作者信息]|[恩墨学院 OCM培训传DBA成功之道]链接:http://www.eygle.co ...
- vue生命周期以及vue的计算属性
一.Vue生命周期(vue实例从创建到销毁的过程,称为生命周期,共有八个阶段) 1.beforeCreate :在实例初始化之后,数据观测 (data observer) 和 event/watche ...
- gdb各种调试命令和技巧
陈皓:用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在UNIX平台 ...
- APP数据埋点分类方式
1.数据埋点的重要性 在现实工作中,数据的整体流程为:数据生产-数据采集-数据处理-数据分析和挖掘-数据可视化,其中,数据采集是很重要的一个环节,数据采集得全不全.对不对,直接决定数据广度和质 ...
- 10 Spring框架 AOP (三) Spring对AspectJ的整合
上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...
- 113. Path Sum II(求等于某个数的所有路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [国家集训队] Crash 的文明世界(第二类斯特林数)
题目 [国家集训队] Crash 的文明世界 前置 斯特林数\(\Longrightarrow\)斯特林数及反演总结 做法 \[\begin{aligned} ans_x&=\sum\limi ...
- 程序员、架构师、技术总监、CTO
程序员 程序员,英文名coder/programmer,大家常自嘲叫码农的阶段.这个角色职责是把需求或产品实现为用户可用的软件产品. 此职位为执行级别.另外因为经验较少,一般需要求助别人,或与别人一起 ...
- [pixhawk笔记]3-架构概览
本文主要内容翻译自:https://dev.px4.io/en/concept/architecture.html 总体架构: PX4代码由两层组成:PX4飞行栈和PX4中间件.其中,前者是一套飞行控 ...
- Your app uses or references the following non-public APIs的解决方案
之前接了一个旧的项目,代码混乱,年代久远,不得不吐槽一波,好不容易改完需求提交代码,说用到了non-public APIs,搞了好久终于找到地方了,下面是我的解决过程,让大家少走弯路: 下面的被驳回的 ...