commons-collections常用工具类

简介: commons-collections常用工具类

一、CollectionUtils

1.集合判断:

例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

2.并集

union()方法

@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]
}

3.交集

intersection()方法

@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]

}

4.交集的补集(析取)

@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]
}

5.差集(扣除)

@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]

}

6.集合是否相等(相当于==号)

  • 集合是否为空
@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
}

7.不可修改的集合

我们对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)

二、MapUtils

isEmpty()可以用于map判断null和size为0, getString()也可以直接获取map中的值为指定类型,没有的返回null

Map map = null;
Map map2 = new HashMap();
Map map3 = new HashMap<>();
map3.put("xxx", "xxx");
// 检验为empty可以验证null和size为0的情况
System.out.println(MapUtils.isEmpty(map));  //true
System.out.println(MapUtils.isEmpty(map2));  //true
System.out.println(MapUtils.isEmpty(map3));  //false

String string = MapUtils.getString(map3, "eee");
String string2 = MapUtils.getString(map3, "xxx");
Integer integer = MapUtils.getInteger(map3, "xxx");
System.out.println("string->" + string);
System.out.println("string2->" + string2);
System.out.println("integer->" + integer);
System.out.println(integer == null);

输出结果为:

true
true
false
INFO: Exception: java.text.ParseException: Unparseable number: "xxx"
string->null
string2->xxx
integer->null
true
  • MapUtils.isEmpty根踪源码:
public static boolean isEmpty(Map map) {
    return (map == null || map.isEmpty());
}

其中map.isEmpry()代码为
public boolean isEmpty() {
        return size == 0;
    }
  • 补充:MapUtils也可以获取值作为String,获取不到取默认值:
//获取字符串,如果获取不到可以返回一个默认值
String string3 = MapUtils.getString(map3, "eee","没有值");

源码分析:

/**
 *  Looks up the given key in the given map, converting the result into
 *  a string, using the default value if the the conversion fails.
 *
 *  @param map  the map whose value to look up
 *  @param key  the key of the value to look up in that map
 *  @param defaultValue  what to return if the value is null or if the
 *     conversion fails
 *  @return  the value in the map as a string, or defaultValue if the 
 *    original value is null, the map is null or the string conversion
 *    fails
 */
public static String getString( Map map, Object key, String defaultValue ) {
    String answer = getString( map, key );
    if ( answer == null ) {
        answer = defaultValue;
    }
    return answer;
}
目录
相关文章
|
消息中间件 存储 中间件
【消息中间件】详解三大MQ:RabbitMQ、RocketMQ、Kafka
【消息中间件】详解三大MQ:RabbitMQ、RocketMQ、Kafka
11064 1
|
存储 缓存 监控
【JVM调优】如何进行JVM调优?一篇文章就够了!
深入解读JVM性能的监控、定位和调优方案,阐述jps/stat/jstack、MAT等常用性能分析工具的使用,提出JVM参数、内存溢出、内存泄漏、CPU飙升、GC频繁等实际场景下JVM调优的方案。
3658 16
【JVM调优】如何进行JVM调优?一篇文章就够了!
|
11月前
|
存储 NoSQL 中间件
单点登录的原理、实现、以及技术方案比较详解
本文详细介绍单点登录(SSO)的定义、原理、实现细节,探讨其在大型网站中的应用,帮助读者理解如何通过分布式Session实现高效的用户认证与授权。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
单点登录的原理、实现、以及技术方案比较详解
|
JSON 前端开发 Java
List<String> 如何传参
List<String> 如何传参
1051 0
|
缓存 Nacos 微服务
Nacos2.2.3版本,微服务注册上去了,然后关闭,注册实例还是显示健康,一直在那,要等大概一小时以后才消失
Nacos2.2.3版本,微服务注册上去了,然后关闭,注册实例还是显示健康,一直在那,要等大概一小时以后才消失,怎么解决?
953 1
|
资源调度 分布式计算 Hadoop
搭建YARN集群
文章介绍了如何搭建YARN集群,包括启动HDFS集群、修改YARN配置文件、启动ResourceManager和NodeManager节点,并提供了用于管理Hadoop集群的自定义脚本。
247 3
|
开发框架 前端开发 中间件
聊聊 ASP.NET Core 中间件(二):中间件和筛选器的区别
聊聊 ASP.NET Core 中间件(二):中间件和筛选器的区别
292 1
java开启线程的四种方法
这篇文章介绍了Java中开启线程的四种方法,包括继承Thread类、实现Runnable接口、实现Callable接口和创建线程池,每种方法都提供了代码实现和测试结果。
java开启线程的四种方法
|
存储 并行计算 数据处理
蓝易云 - 香港站群服务器1C,4C,8C,16C的配置介绍
以上是对服务器CPU核心数量的基本解释,但实际的服务器配置还会包括其他因素,如内存大小、存储类型和网络带宽等。这些因素都会影响服务器的性能和适用场景。
983 2
|
SQL 关系型数据库 MySQL
实时计算 Flink版产品使用问题之JdbcSink是否支持将数据写入到MySQL数据库中
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。