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.最近才发现此工具类还可以取集合的交集.并集.甚至差集 ...
随机推荐
- 分页和Cookie、Session
分页和Cookie.Session 分页 自定义分页 函数班 from django.shortcuts import render # Create your views here. data = ...
- python之元组操作
1.元组创建 tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", &qu ...
- T25健身视频全集+课表
http://jianfei.39.net/thread-3639251-1.html T25健身视频全集+课表 强度适中 不伤膝盖! [复制链接] zytttt 主题 好友 ...
- arcgis中给属性文件加x y坐标
两种方式: 一, 1在ArcGIS 9.2桌面软件arcview级别以上软件中,加载要添加x,y坐标的数据,打开属性表,添加X.Y字段 2 右键X字段,选择calculate geometry,如果颜 ...
- 发送邮件——stamplib
配置文email.ini件信息: [email]sender=xxxxxxxxxxxpwd=xxxxxxxxxxxxreciver=xxxxxxxxxxxxxpython 3.x代码如下: impor ...
- NGUI如何使用汉字
1:准备好字体文件,就是ttf后缀名的那些.. 2:在第一个红线部分,将下拉框选择为Unity,在后面的字体里面选择第一步准备好的字体. 3:创建UILabel,widget里面的Color才是字体的 ...
- 20145219 《Java程序设计》第10周学习总结
20145219 <Java程序设计>第10周学习总结 教材学习内容总结 Java的网络编程 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 网络概述 1.计算机 ...
- 使用buildroot创建自己的交叉编译工具链【转】
本文转载自:https://blog.csdn.net/linczone/article/details/45894181 使用buildroot创建自己的交叉编译工具链 关键字:buildroot ...
- HDU 5703
题意:给你一个数n,问将n分为正整数和的方案数.如n=3共四种,1 1 1 , 1 2 , 2 1 ,3 . 思路:隔板法,n个1,有n-1个空位,每个空位可以选择是否插入隔板,插入k(0<=k ...
- Effective C++ 条款02:尽量以const,enum,inline替换 #define
换一种说法就是宁可以编译器替换预处理器 举例 #define ASPECT_RATIO 1.653 记号ASPECT_RATIO也许从未被编译器看见:也许在编译起开始处理源码前它就被预处理器移走了,于 ...