飞道的博客

Springboot内置的工具类之CollectionUtils

652人阅读  评论(0)

前言

        实际业务开发中,集合的判断和操作也是经常用到的,Spring也针对集合的判断和操作封装了一些方法,但是最令我惊讶的是,我在梳理这些内容的过程中发现了一些有趣的现象,我的第一反应是不敢相信,再想一想,没错,我是对的。所以强烈建议大家可以认真看完这篇文章,这一篇绝对有价值,因为有趣的是我我竟然发现了Spring的两个bug。

 org.springframework.util.CollectionUtils

集合的判断

boolean hasUniqueObject(Collection collection)

从源码注释上看,是用于判断 List/Set 中的每个元素是否唯一,即 List/Set 中不存在重复元素。但这里要告诉大家千万不要用这个方法,因为这个方法有bug,为什么呢?下面是Spring-core-5.2.13.RELEASE.jar中的源码,且看12行,细心的人会发现两个对象之间比较是否相等用的是!=。还记得“==”和“equals”的区别吗?“==”操作符专门用来比较两个变量的值是否相等,equals()方法是用于比较两个独立对象的内容是否相同。所以这里如果集合中的元素是数值,可以用“==”比较,如果是普通的引用对象,就得不到正确的结果了。


  
  1. public static boolean hasUniqueObject( Collection<?> collection) {
  2. if ( isEmpty(collection)) {
  3. return false;
  4. }
  5. boolean hasCandidate = false;
  6. Object candidate = null;
  7. for ( Object elem : collection) {
  8. if (!hasCandidate) {
  9. hasCandidate = true;
  10. candidate = elem;
  11. }
  12. else if (candidate != elem) {
  13. return false;
  14. }
  15. }
  16. return true;
  17. }

boolean containsInstance(Collection collection, Object element)

从源码的注释上看,是用于判断集合中是否包含某个对象。这个方法也不建议使用,因为与上一个方法存在相同的问题,且看源码的第4行,依然用的是“==”。


  
  1. public static boolean containsInstance( @Nullable Collection<?> collection, Object element) {
  2. if (collection != null) {
  3. for ( Object candidate : collection) {
  4. if (candidate == element) {
  5. return true;
  6. }
  7. }
  8. }
  9. return false;
  10. }

boolean isEmpty(Collection collection)

这个方法已验证过可以放心用,用于判断 List/Set 是否为空;


  
  1. @Test
  2. public void test1( ){
  3. Collection< String> list= new ArrayList<>();
  4. boolean empty = CollectionUtils. isEmpty(list);
  5. Assert. isTrue(empty, "集合list不为空");
  6. System. out. println( "集合list增加一元素");
  7. list. add( "happy");
  8. boolean empty2 = CollectionUtils. isEmpty(list);
  9. Assert. isTrue(empty2, "集合list不为空");
  10. }

boolean isEmpty(Map map)

用于判断 Map 是否为空。


  
  1. @Test
  2. public void test2( ){
  3. Map< String, String> map = new HashMap<>();
  4. boolean empty = CollectionUtils. isEmpty(map);
  5. Assert. isTrue(empty, "map不为空");
  6. System. out. println( "map中增加元素");
  7. map. put( "name", "jack");
  8. boolean empty2 = CollectionUtils. isEmpty(map);
  9. Assert. isTrue(empty2, "map不为空");
  10. }

boolean containsAny(Collection source, Collection candidates)

从源码上的注释看,是用于判断集合source中是否包含另一个集合candidates的任意一个元素,即集合candidates中的元素是否完全包含于集合soruce。

从源码这个方法中的元素之间的比较用到了“equals”方法,且调用的是集合内对象的equals方法,因此使用这个方法想要得到正确的结果的前提是,比较的对象要重写hashCode()和eauals()方法。


  
  1. @Test
  2. public void test4 (){
  3. Employee lisi = new Employee( "lisi");
  4. Employee zhangsan = new Employee( "zhangsan");
  5. Employee wangwu = new Employee( "wangwu");
  6. List<Employee > list= new ArrayList<>();
  7. list.add(zhangsan);
  8. list.add(lisi);
  9. List<Employee> list2= new ArrayList<>();
  10. list2.add(wangwu);
  11. //这里可以用是因为比较的时候调用的是equals方法
  12. boolean b = CollectionUtils.containsAny(list, list2);
  13. Assert.isTrue(b, "list1没有包含有list2中任意一个元素");
  14. }

集合的操作

void mergeArrayIntoCollection(Object array, Collection collection)

将数组array中的元素都添加到 List/Set 中。


  
  1. @Test
  2. public void test6 (){
  3. List<Employee > list= new ArrayList<>();
  4. Employee lisi = new Employee( "lisi");
  5. list.add(lisi);
  6. Employee zhangsan = new Employee( "zhangsan");
  7. Employee[] employees={zhangsan};
  8. CollectionUtils.mergeArrayIntoCollection(employees, list);
  9. Assert.isTrue(list.size()== 2, "把数据中的元素合并到list失败了");
  10. }

void mergePropertiesIntoMap(Properties props, Map map)

将 Properties 中的键值对都添加到 Map 中。


  
  1. @Test
  2. public void test7( ){
  3. Properties properties = new Properties();
  4. properties. setProperty( "name", "zhangsan");
  5. Map< String, String > map = new HashMap<>();
  6. CollectionUtils. mergePropertiesIntoMap(properties, map);
  7. Assert. isTrue(map. get( "name"). equals( "zhangsan"), "把properties中的元素合并到map中失败了");
  8. }

T lastElement(List list)

返回 List 中最后一个元素。


  
  1. @Test
  2. public void test8(){
  3. Employee lisi = new Employee( "lisi");
  4. Employee zhangsan = new Employee( "zhangsan");
  5. List<Employee > list= new ArrayList<>();
  6. list. add(zhangsan);
  7. list. add(lisi);
  8. Employee employee = CollectionUtils.lastElement(list);
  9. Assert.isTrue(employee. equals(lisi), "获取集合最后一个元素失败了");
  10. }

T firstElement(List list)

返回集合中第一个元素。


  
  1. @Test
  2. public void test9(){
  3. Employee lisi = new Employee( "lisi");
  4. Employee zhangsan = new Employee( "zhangsan");
  5. List<Employee > list= new ArrayList<>();
  6. list. add(zhangsan);
  7. list. add(lisi);
  8. Employee employee = CollectionUtils.firstElement(list);
  9. Assert.isTrue(employee. equals(zhangsan), "获取集合第一个元素失败了");
  10. }

List arrayToList(Object source)

把一个数组转换成一个集合。


  
  1. @Test
  2. public void test10 (){
  3. Employee zhangsan = new Employee( "zhangsan");
  4. Employee[] employees={zhangsan};
  5. List list = CollectionUtils.arrayToList(employees);
  6. Assert.isTrue(list.size()== 1, "把数据转换成集合失败了");
  7. }

伙计,看完赶紧收藏+关注,省得用的时候找不着!


转载:https://blog.csdn.net/fox9916/article/details/128321098
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场