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.最近才发现此工具类还可以取集合的交集.并集.甚至差集 ...
随机推荐
- oradebug工具使用3(转载)
1 oradebug介绍 oradebug主要是给oracle支持人员使用的,尽管很早便有,但oracle官网很少有记载.他是个sql*plus命令行工具,有sysdba的权限就可以登入,无需特别设置 ...
- jQuery Mobile 手动显示ajax加载器
在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...
- 解释一下python中的逻辑运算符
python中有三个逻辑运算符:and.or.not print(False and True)#False print(7<7 or True)#True print(not 2==2)#Fa ...
- LeetCode:棒球比赛【682】
LeetCode:棒球比赛[682] 题目描述 你现在是棒球比赛记录员.给定一个字符串列表,每个字符串可以是以下四种类型之一:1.整数(一轮的得分):直接表示您在本轮中获得的积分数.2. " ...
- PAT 天梯赛 L1-008. 求整数段和 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-008 AC代码 #include <iostream> #include <cstdio&g ...
- 深入理解JVM3
VM运行时数据区域 JVM执行Java程序的过程中,会使用到各种数据区域,这些区域有各自的用途.创建和销毁时间.根据<Java虚拟机规范(第二版)>的规定,JVM包括下列几个运行时数据区域 ...
- TRUNC函数的用法
TRUNC函数用于对值进行截断. 用法有两种:TRUNC(NUMBER)表示截断数字,TRUNC(date)表示截断日期. (1)截断数字: 格式:TRUNC(n1,n2),n1表示被截断的数字,n2 ...
- Makefile的简单编写【学习笔记】
首先我们先创建两个简单的文件: main.c #include <stdio.h> extern void hi_fun(); int main() { printf("hell ...
- 深入解析Linux内核及其相关架构的依赖关系
Linux kernel 成功的两个原因: 灵活的架构设计使得大量的志愿开发者能够很容易加入到开发过程中:每个子系统(尤其是那些需要改进的)都具备良好的可扩展性.正是这两个原因使得Linux kern ...
- Mysql导出导入数据库
1. 导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u root -p database > database.sql 2. 导入数 ...