commons-collections包中的常用的工具类

       <dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>

1. CollectionUtils工具类用于操作集合,  isEmpty () 方法最有用   (commons-collections包中的类)

package cn.xm.exam.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import org.apache.commons.collections.CollectionUtils; public class test {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("str1");
list.add("str2"); List<String> list1 = new ArrayList<String>();
list1.add("str1");
list1.add("str21"); // 判断是否有任何一个相同的元素
System.out.println(CollectionUtils.containsAny(list, list1)); // 求并集(自动去重)
List<String> list3 = (List<String>) CollectionUtils.union(list, list1);
System.out.println(list3); // 求交集(两个集合中都有的元素)
Collection intersection = CollectionUtils.intersection(list, list1);
System.out.println("intersection->" + intersection); // 求差集(并集去掉交集,也就是list中有list1中没有,list1中有list中没有)
Collection intersection1 = CollectionUtils.disjunction(list, list1);
System.out.println("intersection1->" + intersection1); // 获取一个同步的集合
Collection synchronizedCollection = CollectionUtils.synchronizedCollection(list); // 验证集合是否为null或者集合的大小是否为0,同理有isNouEmpty方法
List list4 = null;
List list5 = new ArrayList<>();
System.out.println(CollectionUtils.isEmpty(list4));
System.out.println(CollectionUtils.isEmpty(list5));
}
}

结果:

true
[str2, str21, str1]
intersection->[str1]
intersection1->[str2, str21]
true
true

补充:此工具类还可以向集合中加数组元素

        List<String> list = new ArrayList<>();
String s[] = { "1", "2" };
CollectionUtils.addAll(list, s);
list.add("3");
System.out.println(list);

结果:

[1, 2, 3]

2.   MapUtils工具类,isEmpty最有用(commons-collections包中的类)

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

package cn.xm.exam.test;

import java.util.HashMap;
import java.util.Map; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.NumberUtils; import ognl.MapElementsAccessor; public class test {
public static void main(String[] args) {
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));
System.out.println(MapUtils.isEmpty(map2));
System.out.println(MapUtils.isEmpty(map3)); 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.isEmpty()代码查看hashmap:
    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;
}

commons-collections包中的常用的工具类的更多相关文章

  1. Hutool中那些常用的工具类和方法

    Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...

  2. C# 中那些常用的工具类(Utility Class)(二)

    今天按照这一年来经常用到的那些静态的工具类再来做一次总结,这些小的工具来可以作为自己学习的很好的例子,通过总结这些东西,能够很大程度上梳理自己的知识体系,当然这个是经常用到的,接下来就一个个去分析这些 ...

  3. 利用commons-io.jar包中FileUtils和IOUtils工具类操作流及文件

    1.String IOUtils.toString(InputStream input),传入输入流对象,返回字符串,有多重重载,可按需要传参 用例: @Test public void showIn ...

  4. C#中那些常用的工具类(Utility Class)(一)

    代码越写越多,但是我们也需要经常去反思那些写过的代码,Utility Class就是这一类需要特别去反思总结的类,这些类像工具一样,我们经常通过一些静态方法,通过传入一些参数,然后得到我们需要的结果, ...

  5. C# 中那些常用的工具类(Utility Class)(三)

    今天来接着写这个系列的文章,这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候 ...

  6. Java语言Lang包下常用的工具类介绍_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都 ...

  7. apache commons lang包中的StringUtils

    计算一个字符串某个字符的出现次数 a, 使用charAt方法截取之后,循环判断. b, 使用apache commons lang包中的StringUtils: int n = StringUtils ...

  8. commons-lang包中我们常用的类的作用

    commons-lang包中对我们有用的类主要有: 1.StringUtils 该类主要提供对字符串的操作,对null是安全的,主要提供了字符串查找,替换,分割,去空白,去掉非法字符等等操作 2.Ob ...

  9. Android常用的工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...

随机推荐

  1. linux的进程间通信概述

    一 进程间通信 1.1. linux内核提供多种进程间通信机制 a. 无名管道和有名管道 b. SystemV IPC:信号量.消息队列.共享内存 c. Socket域套接字 d. 信号 1.2. 无 ...

  2. RabbitMq学习2-php命令行模式测试rabbitmq

    一.RabbitMQ结构 1.几个概念说明:       Broker:简单来说就是消息队列服务器实体. Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列. Queue:消息队列载体 ...

  3. Laravel 学习笔记之文件上传

    自定义添加磁盘——upload 位置:config/filesystems.php 'disks' => [ 'local' => [ 'driver' => 'local', 'r ...

  4. datatable和dataset的区别

    DataSet 是离线的数据源 DataTable 是数据源中的表.当然也可以自己建一张虚表.插入数据库中 DataSet是DataTable的容器DataSet可以比作一个内存中的数据库,DataT ...

  5. postman中x-www-form-urlencoded与form-data的区别

    这是W3C定义的两种不同的表格类型,如果你想发送简单的text/ASCII数据,使用x-www-form-urlencoded , 这是默认的形式. 如果你想发送非ASCII文本或者大的二进制数据,使 ...

  6. Apache 配置内网站点

    基于端口常用于内部网站,不对外开发的网站 [root@Nagios-Server extra]# vim ../httpd.conf Listen 80 Listen 8000 Listen 9000 ...

  7. vue + nginx部署404

    记录: vue项目打包放到服务器,使用nginx反向代理的时候路由页面刷新报404,因为刷新时,不存在路径对应的文件或文件夹,需要在nginx配置中添加代码: index index.html; tr ...

  8. 分页控件SSTab

    一.分页控件SSTab概述1.作用:采用分页形式查询或编辑数据表中数据.2.添加到控件箱菜单命令:工程 | 部件,选择:Microsoft Tabbed Dialog Control 6.0 (SP6 ...

  9. Linux之目录与路径

    特殊的目录: “.”,代表此层目录 “..”,代表上一层目录 “-”,代表前一个工作目录 “~”,代表“目前用户身份”所在的主文件夹 “~account”,代表account这个用户的主文件夹(acc ...

  10. AtCoder Regular Contest 092 Two Sequences AtCoder - 3943 (二进制+二分)

    Problem Statement You are given two integer sequences, each of length N: a1,…,aN and b1,…,bN. There ...