org.springframework.util.CollectionUtils

集合判断工具

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

  

集合操作工具

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 @ 2022-02-07 11:27  草木物语  阅读(701)  评论(0)    收藏  举报