iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Springboot内置的工具类CollectionUtils怎么使用
  • 298
分享到

Springboot内置的工具类CollectionUtils怎么使用

2023-07-04 19:07:03 298人浏览 安东尼
摘要

这篇“SpringBoot内置的工具类CollectionUtils怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“s

这篇“SpringBoot内置的工具类CollectionUtils怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“springboot内置的工具类CollectionUtils怎么使用”文章吧。

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怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。

--结束END--

本文标题: Springboot内置的工具类CollectionUtils怎么使用

本文链接: https://www.lsjlt.com/news/347165.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作