org.springframework.util.collectionutils集合的判断boolean hasuniqueobject(collection collection)
从源码注释上看,是用于判断 list/set 中的每个元素是否唯一,即 list/set 中不存在重复元素。但这里要告诉大家千万不要用这个方法,因为这个方法有bug,为什么呢?下面是spring-core-5.2.13.release.jar中的源码,且看12行,细心的人会发现两个对象之间比较是否相等用的是!=。还记得“==”和“equals”的区别吗?“==”操作符专门用来比较两个变量的值是否相等,equals()方法是用于比较两个独立对象的内容是否相同。所以这里如果集合中的元素是数值,可以用“==”比较,如果是普通的引用对象,就得不到正确的结果了。
public static boolean hasuniqueobject(collection<?> collection) { if (isempty(collection)) { return false; } boolean hascandidate = false; object candidate = null; for (object elem : collection) { if (!hascandidate) { hascandidate = true; candidate = elem; } else if (candidate != elem) { return false; } } return true;}
boolean containsinstance(collection collection, object element)
从源码的注释上看,是用于判断集合中是否包含某个对象。这个方法也不建议使用,因为与上一个方法存在相同的问题,且看源码的第4行,依然用的是“==”。
public static boolean containsinstance(@nullable collection<?> collection, object element) { if (collection != null) { for (object candidate : collection) { if (candidate == element) { return true; } } } return false;}
boolean isempty(collection collection)
这个方法已验证过可以放心用,用于判断 list/set 是否为空;
@testpublic void test1(){ collection<string> list=new arraylist<>(); boolean empty = collectionutils.isempty(list); assert.istrue(empty, "集合list不为空"); system.out.println("集合list增加一元素"); list.add("happy"); boolean empty2 = collectionutils.isempty(list); assert.istrue(empty2, "集合list不为空");}
boolean isempty(map map)
用于判断 map 是否为空。
@testpublic void test2(){ map<string,string> map = new hashmap<>(); boolean empty = collectionutils.isempty(map); assert.istrue(empty, "map不为空"); system.out.println("map中增加元素"); map.put("name", "jack"); boolean empty2 = collectionutils.isempty(map); assert.istrue(empty2, "map不为空");}
boolean containsany(collection source, collection candidates)
从源码上的注释看,是用于判断集合source中是否包含另一个集合candidates的任意一个元素,即集合candidates中的元素是否完全包含于集合soruce。
从源码这个方法中的元素之间的比较用到了“equals”方法,且调用的是集合内对象的equals方法,因此使用这个方法想要得到正确的结果的前提是,比较的对象要重写hashcode()和eauals()方法。
@testpublic void test4(){ employee lisi = new employee("lisi"); employee zhangsan = new employee("zhangsan"); employee wangwu = new employee("wangwu"); list<employee > list=new arraylist<>(); list.add(zhangsan); list.add(lisi); list<employee> list2=new arraylist<>(); list2.add(wangwu); //这里可以用是因为比较的时候调用的是equals方法 boolean b = collectionutils.containsany(list, list2); assert.istrue(b, "list1没有包含有list2中任意一个元素");}
集合的操作void mergearrayintocollection(object array, collection collection)
将数组array中的元素都添加到 list/set 中。
@testpublic void test6(){ list<employee > list=new arraylist<>(); employee lisi = new employee("lisi"); list.add(lisi); employee zhangsan = new employee("zhangsan"); employee[] employees={zhangsan}; collectionutils.mergearrayintocollection(employees, list); assert.istrue(list.size()==2, "把数据中的元素合并到list失败了");}
void mergepropertiesintomap(properties props, map map)
将 properties 中的键值对都添加到 map 中。
@testpublic void test7(){ properties properties = new properties(); properties.setproperty("name", "zhangsan"); map<string,string > map = new hashmap<>(); collectionutils.mergepropertiesintomap(properties, map); assert.istrue(map.get("name").equals("zhangsan"), "把properties中的元素合并到map中失败了");}@testpublic void test7(){ properties properties = new properties(); properties.setproperty("name", "zhangsan"); map<string,string > map = new hashmap<>(); collectionutils.mergepropertiesintomap(properties, map); assert.istrue(map.get("name").equals("zhangsan"), "把properties中的元素合并到map中失败了");}
t lastelement(list list)
返回 list 中最后一个元素。
@testpublic void test9(){ employee lisi = new employee("lisi"); employee zhangsan = new employee("zhangsan"); list<employee > list=new arraylist<>(); list.add(zhangsan); list.add(lisi); employee employee = collectionutils.firstelement(list); assert.istrue(employee.equals(zhangsan), "获取集合第一个元素失败了"); }
t firstelement(list list)
返回集合中第一个元素。
@testpublic void test10(){ employee zhangsan = new employee("zhangsan"); employee[] employees={zhangsan}; list list = collectionutils.arraytolist(employees); assert.istrue(list.size()==1, "把数据转换成集合失败了");}
list arraytolist(object source)
把一个数组转换成一个集合。
@testpublic void test10(){ employee zhangsan = new employee("zhangsan"); employee[] employees={zhangsan}; list list = collectionutils.arraytolist(employees); assert.istrue(list.size()==1, "把数据转换成集合失败了");}
以上就是springboot内置的工具类collectionutils怎么使用的详细内容。