集合判断: 
  例1: 判断集合是否为空:

CollectionUtils.isEmpty(null): true
  CollectionUtils.isEmpty(new ArrayList()): true  
  CollectionUtils.isEmpty({a,b}): false

  例2: 判断集合是否不为空:

 CollectionUtils.isNotEmpty(null): false
  CollectionUtils.isNotEmpty(new ArrayList()): false
  CollectionUtils.isNotEmpty({a,b}): true

CollectionUtils在真实项目中,是一个非常好用的工具类,使用非常频繁。它可以使代码更加简洁和安全。刚好在工作中利用这个工具类重构代码,顺便总结下分享分享:

并集
@Test
public void testUnion(){
String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
//2个数组取并集
System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB)));
//[A, B, C, D, E, F, G, H, K]
}
交集
@Test
public void testIntersection(){
String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
//2个数组取交集
System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB)));
//[B, D, F] }
交集的补集(析取)
@Test
public void testDisjunction(){
String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
//2个数组取交集 的补集
System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB)));
//[A, C, E, G, H, K]
}
差集(扣除)
@Test
public void testSubtract(){
String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };
String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
List<String> listA = Arrays.asList(arrayA);
List<String> listB = Arrays.asList(arrayB);
//arrayA扣除arrayB
System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB)));
//[A, C, E] }
集合是否为空
@Test
public void testIsEmpty(){ class Person{}
class Girl extends Person{} List<Integer> first = new ArrayList<>();
List<Integer> second = null;
List<Person> boy = new ArrayList<>();
//每个男孩心里都装着一个女孩
boy.add(new Girl());
//判断集合是否为空
System.out.println(CollectionUtils.isEmpty(first)); //true
System.out.println(CollectionUtils.isEmpty(second)); //true
System.out.println(CollectionUtils.isEmpty(boy)); //false //判断集合是否不为空
System.out.println(CollectionUtils.isNotEmpty(first)); //false
System.out.println(CollectionUtils.isNotEmpty(second)); //false
System.out.println(CollectionUtils.isNotEmpty(boy)); //true
}
集合是否相等
@Test
public void testIsEqual(){ class Person{}
class Girl extends Person{
} List<Integer> first = new ArrayList<>();
List<Integer> second = new ArrayList<>();
first.add(1);
first.add(2);
second.add(2);
second.add(1);
Girl goldGirl = new Girl();
List<Person> boy1 = new ArrayList<>();
//每个男孩心里都装着一个女孩
boy1.add(new Girl());
List<Person> boy2 = new ArrayList<>();
//每个男孩心里都装着一个女孩
boy2.add(new Girl());
//比较两集合值
System.out.println(CollectionUtils.isEqualCollection(first,second)); //true
System.out.println(CollectionUtils.isEqualCollection(first,boy1)); //false
System.out.println(CollectionUtils.isEqualCollection(boy1,boy2)); //false List<Person> boy3 = new ArrayList<>();
//每个男孩心里都装着一个女孩
boy3.add(goldGirl);
List<Person> boy4 = new ArrayList<>();
boy4.add(goldGirl);
System.out.println(CollectionUtils.isEqualCollection(boy3,boy4)); //true
}
不可修改的集合

我们对c进行操作,s也同样获得了和c相同的内容,这样就可以避免其他人员修改这个s对象。有时候需要对它进行保护,避免返回结果被人修改。

@Test
public void testUnmodifiableCollection(){
Collection<String> c = new ArrayList<>();
Collection<String> s = CollectionUtils.unmodifiableCollection(c);
c.add("boy");
c.add("love");
c.add("girl");
//! s.add("have a error");
System.out.println(s);
}

Collections.unmodifiableCollection可以得到一个集合的镜像,它的返回结果是不可直接被改变,否则会提示错误

java.lang.UnsupportedOperationException
at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)

CollectionUtils工具类的常用方法的更多相关文章

  1. StringUtils、CollectionUtils工具类的常用方法

    唯能极于情,故能极于剑 欢迎来到 “程序牛CodeCow” 的博客,有问题请及时关注小编公众号 “CodeCow”,大家一起学习交流 下面将为大家演示StringUtils.CollectionUti ...

  2. CollectionUtils工具类中常用方法

    @SuppressWarnings("rawtypes") @Test public void test1() { List<String> coll = new Ar ...

  3. java代码之美(12)---CollectionUtils工具类

    java代码之美(12)---CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的, 而不是springframework下的CollectionUt ...

  4. CollectionUtils工具类

    CollectionUtils工具类 这篇讲的CollectionUtils工具类是在apache下的,可以使代码更加简洁和安全. 使用前需导入依赖 <dependency> <gr ...

  5. java代码(12) ---CollectionUtils工具类

    CollectionUtils工具类 CollectionUtils工具类是在apache下的,而不是springframework下的CollectionUtils 个人觉得在真实项目中Collec ...

  6. 基于StringUtils工具类的常用方法介绍(必看篇)

    前言:工作中看到项目组里的大牛写代码大量的用到了StringUtils工具类来做字符串的操作,便学习整理了一下,方便查阅. isEmpty(String str) 是否为空,空格字符为false is ...

  7. 通过CollectionUtils工具类判断集合是否为空,通过StringUtils工具类判断字符串是否为空

    通过CollectionUtils工具类判断集合是否为空 先引入CollectionUtils工具类: import org.apache.commons.collections4.Collectio ...

  8. CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2)

    一.CollectionUtils工具类之并集union(arr1,arr2)和差集subtract(arr1,arr2) 采用的类: import org.apache.commons.collec ...

  9. Collections集合工具类的常用方法

    Collections集合工具类的方法 addAll与shuffle import java.util.ArrayList; import java.util.Collections; /* - ja ...

随机推荐

  1. React多层级表单

    因项目需要封装的组件,组件库使用的是Ant Design 用到了 Form组件 , 布局组件,表单控件 ,如果没有使用Ant Design,可以用rc-form代替,需要对组件中使用的表单控件和布局进 ...

  2. Oracle数据库启动出现ORA-27101错误之ORA-19815处理方式及数据备份

    ORA-27101: sharedmemory realm does not exist之ORA-19815处理 重启数据库(数据库:muphy),登陆是越到错误: ORA-27101: shared ...

  3. docker-lnmp dockerfile

    code: FROM php:7.1.26-fpm WORKDIR /usr/share/nginx/html # bcmath pdo_mysql intl gd zip opcache xdebu ...

  4. MT【328】向量里的最佳逼近

    已知平面向量$\overrightarrow {a},\overrightarrow {b}$满足$|\overrightarrow {a}|=4,|\overrightarrow {b}|=2$.若 ...

  5. Linux keepalived工作原理

    keepalived简介与工作原理 Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他 ...

  6. 集合源码分析[3]-ArrayList 源码分析

    历史文章: Collection 源码分析 AbstractList 源码分析 介绍 ArrayList是一个数组队列,相当于动态数组,与Java的数组对比,他的容量可以动态改变. 继承关系 Arra ...

  7. LOJ#2244 起床困难综合症

    解:m = 0的部分分,直接模拟.有and 0的部分分,直接模拟.<=1000的部分分,枚举攻击力之后模拟.所有操作相同的部分分,可以合并成只有一个操作.然后枚举m或者逐位贪心. 正解是逐位贪心 ...

  8. linux device drivers ch03

    ch03.字符设备驱动程序 编写驱动程序的第一步就是定义驱动程序为用户程序提供的能力(机制).接下来以scull(“Simple Character Utility for Loading Local ...

  9. (N叉树 DFS 递归 BFS) leetcode 559. Maximum Depth of N-ary Tree

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  10. ROS time stamp and sync

    1. https://answers.ros.org/question/189867/what-is-the-timestamp/ In ROS messages timestamp is taken ...