commons-collections包中的常用的工具类
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包中的常用的工具类的更多相关文章
- Hutool中那些常用的工具类和方法
Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...
- C# 中那些常用的工具类(Utility Class)(二)
今天按照这一年来经常用到的那些静态的工具类再来做一次总结,这些小的工具来可以作为自己学习的很好的例子,通过总结这些东西,能够很大程度上梳理自己的知识体系,当然这个是经常用到的,接下来就一个个去分析这些 ...
- 利用commons-io.jar包中FileUtils和IOUtils工具类操作流及文件
1.String IOUtils.toString(InputStream input),传入输入流对象,返回字符串,有多重重载,可按需要传参 用例: @Test public void showIn ...
- C#中那些常用的工具类(Utility Class)(一)
代码越写越多,但是我们也需要经常去反思那些写过的代码,Utility Class就是这一类需要特别去反思总结的类,这些类像工具一样,我们经常通过一些静态方法,通过传入一些参数,然后得到我们需要的结果, ...
- C# 中那些常用的工具类(Utility Class)(三)
今天来接着写这个系列的文章,这一篇主要是用来介绍关于C#中的XML序列化的问题,这个相信大家一定会经常使用它,特别是在WPF中,有时候我们需要将我们后台的数据保存在数据库中,从而在软件下一次启动的时候 ...
- Java语言Lang包下常用的工具类介绍_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 无论你在开发哪中 Java 应用程序,都免不了要写很多工具类/工具函数.你可知道,有很多现成的工具类可用,并且代码质量都 ...
- apache commons lang包中的StringUtils
计算一个字符串某个字符的出现次数 a, 使用charAt方法截取之后,循环判断. b, 使用apache commons lang包中的StringUtils: int n = StringUtils ...
- commons-lang包中我们常用的类的作用
commons-lang包中对我们有用的类主要有: 1.StringUtils 该类主要提供对字符串的操作,对null是安全的,主要提供了字符串查找,替换,分割,去空白,去掉非法字符等等操作 2.Ob ...
- Android常用的工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Prefe ...
随机推荐
- C++中的函数重载分析(一)
1,重载是 C 语言到 C++ 语言的一个飞跃,C 语言中没有重载的概念,所有的函数 名是不允许有重复的,在 C++ 中因为引进了重载,所以函数名可以重复: 2,自然语言中的上下文: 1,你知道上面词 ...
- Simpsons’ Hidden Talents
Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...
- 1000行MySQL学习笔记
/* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */ mysq ...
- [LeetCode] 135. 分发糖果
题目链接 : https://leetcode-cn.com/problems/candy/ 题目描述: 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分 ...
- vue表单校验(三)
vue表单校验(三) 每当看到heyui的这个表单校验,我就一直想将element的校验也做类似的功能,终于有了方式,虽然不是很完美,但是可以使用,能满足要求了 实现方式 基于element-ui实现 ...
- vue filters过滤
<template> <div class="filters"> <h1 v-text="filtersTitle">< ...
- web.xml文件头声明各个版本参考
Servlet 3.1 Java EE 7 XML schema, namespace is http://xmlns.jcp.org/xml/ns/javaee/ <web-app xmlns ...
- Nginx 详细介绍
Nginx 和 Apache 一样是 http 服务器软件.它们的区别是 Apache 的处理速度慢,占用内存资源, nginx 恰恰相反.在功能上 Apache 的所有模块都支持动和静态编译,而 n ...
- 北京太速科技股份有限公司产品手册V201903020
如果您无法正常查看,请点击在线浏览 如果您无法正常查看,请点击在线浏览 了解更多产品信息,请扫描二维码,期待您的关注 ...
- springboot之学习搭建
什么是**SpringBoot?** Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配 ...