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
12805 1
|
存储 缓存 监控
【JVM调优】如何进行JVM调优?一篇文章就够了!
深入解读JVM性能的监控、定位和调优方案,阐述jps/stat/jstack、MAT等常用性能分析工具的使用,提出JVM参数、内存溢出、内存泄漏、CPU飙升、GC频繁等实际场景下JVM调优的方案。
4238 16
【JVM调优】如何进行JVM调优?一篇文章就够了!
|
11月前
|
Java API Spring
Java小抄 使用StopWatch输出执行耗时
通过本文的介绍,我们详细讲解了如何使用 `StopWatch` 类测量代码执行时间。`StopWatch` 提供了简单而强大的功能,帮助我们精确分析代码的性能瓶颈,优化程序效率。希望本文能帮助您更好地理解和应用 `StopWatch`,在实际开发中提高代码性能和质量。
1883 80
|
存储 NoSQL 中间件
单点登录的原理、实现、以及技术方案比较详解
本文详细介绍单点登录(SSO)的定义、原理、实现细节,探讨其在大型网站中的应用,帮助读者理解如何通过分布式Session实现高效的用户认证与授权。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
单点登录的原理、实现、以及技术方案比较详解
|
5月前
|
Ubuntu 安全 Linux
Ubuntu 22.04.5 LTS发布,新硬件支持成亮点
用户可根据个人需求选择相应的版本进行下载,并遵循安装指南进行全新安装或通过USB设备创建启动盘进行系统部署。对于在服务器或企业环境中部署Ubuntu的用户,建议选用Ubuntu 22.04.5 LTS Server版本,它专为服务器优化并配备了强大的网络与安全工具。
|
前端开发 JavaScript 持续交付
前端技术趋势:2024年值得关注的几个方面
【10月更文挑战第9天】前端技术趋势:2024年值得关注的几个方面
|
Java Spring
SpringBoot2.7.18拦截器失效不起作用
本文记录了作者在配置Spring Boot项目中的拦截器时遇到的问题。通过复制和修改其他项目的拦截器代码,但发现拦截器始终不生效。最终发现问题出在`WebConfig.java`中配置路径模式的方式上,即在已设置`context-path`的情况下,不应再使用`addPathPatterns(contextPath + &quot;/**&quot;)`。文章提供了详细的配置文件和代码示例,帮助读者理解并避免类似问题。
925 0
常用工具类-Arrays
本文介绍了Java工具类Arrays和Arrays.ArrayList的常用功能,包括数组的打印、创建、比较、排序、检索以及转换为List的方法,以及setAll和parallelSetAll的使用,展示了如何通过这些工具类便捷地操作数组。
常用工具类-Arrays
|
Java UED
基于SpringBoot自定义线程池实现多线程执行方法,以及多线程之间的协调和同步
这篇文章介绍了在SpringBoot项目中如何自定义线程池来实现多线程执行方法,并探讨了多线程之间的协调和同步问题,提供了相关的示例代码。
4306 0
|
安全 Java 编译器
单例模式的4种实现方式
单例模式的4种实现方式
323 0