commons-collections包中的常用的工具类
时间:2019-06-16
本文章向大家介绍commons-collections包中的常用的工具类,主要包括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;
}
原文地址:https://www.cnblogs.com/dw3306/p/11033204.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- iTerm2安装和配置
- 最新最全的mutect2教程
- Flutter基础widgets教程-CupertinoAlertDialog篇
- Spring的Controller是单例还是多例?怎么保证并发的安全
- 用TypeScript装饰器实现一个简单的依赖注入
- PING问题解决方法_20190305
- 前端需要掌握的设计模式
- @vue/composition-api速成课(通俗易懂版)
- 新1期视频第14课und异常中断模式的bug以及对应的解决方法
- 第一课:linux设备树的引入与体验(基于linux4.19内核版本)
- 第二课:linux设备树的规范(dts和dtb)
- 二叉树剪枝了解一下~
- 第三课:linux内核对设备树的处理
- 第四课:u-boot对设备树的支持
- 第五课. 内核中断系统中的设备树