org.springframework.util.CollectionUtils

集合判断工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
List<String> list = new ArrayList<>();
// 判断 集合 是否为空
boolean empty = CollectionUtils.isEmpty(list);
System.out.println(empty);//true
// 判断 集合 中是否包含某个对象
list.add("abc");
boolean abc = CollectionUtils.containsInstance(list, "abc");
System.out.println(abc);//true
// 以迭代器的方式,判断 集合 中是否包含某个对象
boolean abc1 = CollectionUtils.contains(list.iterator(), "abc");
System.out.println(abc1);//true
// 判断 集合 是否包含某些对象中的任意一个
boolean b = CollectionUtils.containsAny(list, list);
System.out.println(b);//true

  

集合操作工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
List<String> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
 
// 将 数组 中的元素都添加到 集合 中
CollectionUtils.mergeArrayIntoCollection(new String[]{"abc", "def"}, list);
System.out.println(list);//[abc, def]
// 将 Properties 中的键值对都添加到 Map 中
Properties properties = new Properties();
properties.put("key1", "abc");
properties.put("key2", "def");
CollectionUtils.mergePropertiesIntoMap(properties, map);
System.out.println(map);//{key1=abc, key2=def}
// 返回参数 candidates 中第一个存在于参数 source 中的元素
String firstMatch = CollectionUtils.findFirstMatch(list, list);
System.out.println(firstMatch);//abc
// 返回 List/Set 中元素的类型
Class<?> commonElementType = CollectionUtils.findCommonElementType(list);
System.out.println(commonElementType);//class java.lang.String

  

 

posted @   草木物语  阅读(665)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-02-07 java 大小写转换
点击右上角即可分享
微信分享提示