最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot內(nèi)置的工具類之CollectionUtils示例講解

 更新時間:2022年12月16日 16:31:48   作者:凡夫販夫  
這篇文章主要介紹了Springboot內(nèi)置的工具類之CollectionUtils,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

        實際業(yè)務開發(fā)中,集合的判斷和操作也是經(jīng)常用到的,Spring也針對集合的判斷和操作封裝了一些方法,但是最令我驚訝的是,我在梳理這些內(nèi)容的過程中發(fā)現(xiàn)了一些有趣的現(xiàn)象,我的第一反應是不敢相信,再想一想,沒錯,我是對的。所以強烈建議大家可以認真看完這篇文章,這一篇絕對有價值,因為有趣的是我我竟然發(fā)現(xiàn)了Spring的兩個bug。

 org.springframework.util.CollectionUtils

集合的判斷

boolean hasUniqueObject(Collection collection)

從源碼注釋上看,是用于判斷 List/Set 中的每個元素是否唯一,即 List/Set 中不存在重復元素。但這里要告訴大家千萬不要用這個方法,因為這個方法有bug,為什么呢?下面是Spring-core-5.2.13.RELEASE.jar中的源碼,且看12行,細心的人會發(fā)現(xiàn)兩個對象之間比較是否相等用的是!=。還記得“==”和“equals”的區(qū)別嗎?“==”操作符專門用來比較兩個變量的值是否相等,equals()方法是用于比較兩個獨立對象的內(nèi)容是否相同。所以這里如果集合中的元素是數(shù)值,可以用“==”比較,如果是普通的引用對象,就得不到正確的結(jié)果了。

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 是否為空;

@Test
public 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 是否為空。

@Test
public 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”方法,且調(diào)用的是集合內(nèi)對象的equals方法,因此使用這個方法想要得到正確的結(jié)果的前提是,比較的對象要重寫hashCode()和eauals()方法。

@Test
public 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);
    //這里可以用是因為比較的時候調(diào)用的是equals方法
    boolean b = CollectionUtils.containsAny(list, list2);
    Assert.isTrue(b, "list1沒有包含有l(wèi)ist2中任意一個元素");
}

集合的操作

void mergeArrayIntoCollection(Object array, Collection collection)

將數(shù)組array中的元素都添加到 List/Set 中。

@Test
public 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, "把數(shù)據(jù)中的元素合并到list失敗了");
}

void mergePropertiesIntoMap(Properties props, Map map)

將 Properties 中的鍵值對都添加到 Map 中。

@Test
public 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中失敗了");
}
@Test
public 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 中最后一個元素。

@Test
public 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)

返回集合中第一個元素。

@Test
public void test10(){
    Employee zhangsan    = new Employee("zhangsan");
    Employee[] employees={zhangsan};
    List list = CollectionUtils.arrayToList(employees);
    Assert.isTrue(list.size()==1, "把數(shù)據(jù)轉(zhuǎn)換成集合失敗了");
}

List arrayToList(Object source)

把一個數(shù)組轉(zhuǎn)換成一個集合。

@Test
public void test10(){
    Employee zhangsan    = new Employee("zhangsan");
    Employee[] employees={zhangsan};
    List list = CollectionUtils.arrayToList(employees);
    Assert.isTrue(list.size()==1, "把數(shù)據(jù)轉(zhuǎn)換成集合失敗了");
}

到此這篇關(guān)于Springboot內(nèi)置的工具類之CollectionUtils的文章就介紹到這了,更多相關(guān)Springboot內(nèi)置的工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot采用jasypt加密配置的項目實踐

    Springboot采用jasypt加密配置的項目實踐

    本文主要介紹了在Spring Boot項目中使用Jasypt對配置文件中的敏感信息進行加密,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-12-12
  • SpringBoot發(fā)送html郵箱驗證碼功能

    SpringBoot發(fā)送html郵箱驗證碼功能

    這篇文章主要介紹了SpringBoot發(fā)送html郵箱驗證碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • 解析spring cloud ouath2中的Eureka

    解析spring cloud ouath2中的Eureka

    這篇文章主要介紹了spring cloud ouath2中的Eureka,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Springboot集成magic-api的詳細過程

    Springboot集成magic-api的詳細過程

    這篇文章主要介紹了Springboot集成magic-api的相關(guān)知識,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • 使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應用

    使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應用

    為了提供一個單包易部署的服務器應用,考慮使用Spring Boot,因為其集成了Apache Tomcat,易于運行,免去絕大部分了服務器配置的步驟
    2017-08-08
  • springboot2.6.7集成springfox3.0.0的示例代碼

    springboot2.6.7集成springfox3.0.0的示例代碼

    這篇文章主要介紹了springboot2.6.7集成springfox3.0.0的示例代碼,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • 5種必會的Java異步調(diào)用轉(zhuǎn)同步的方法你會幾種

    5種必會的Java異步調(diào)用轉(zhuǎn)同步的方法你會幾種

    這篇文章主要介紹了5種必會的Java異步調(diào)用轉(zhuǎn)同步的方法你會幾種,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • SpringBoot如何使用p6spy監(jiān)控數(shù)據(jù)庫

    SpringBoot如何使用p6spy監(jiān)控數(shù)據(jù)庫

    這篇文章主要介紹了SpringBoot如何使用p6spy監(jiān)控數(shù)據(jù)庫問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Nacos框架與原理之Nacos的參數(shù)

    Nacos框架與原理之Nacos的參數(shù)

    這篇文章主要介紹了Nacos框架與原理之Nacos的參數(shù),Nacos?中的參數(shù)有很多,如:命名空間、分組名、服務名、保護閾值、服務路由類型、臨時實例等,但下面文章我們要講解的是參數(shù),參數(shù)是什么呢,下面一起進去文章學習詳細內(nèi)容吧
    2022-05-05
  • Spring?Boot請求處理之常用參數(shù)注解使用教程

    Spring?Boot請求處理之常用參數(shù)注解使用教程

    這篇文章主要給大家介紹了關(guān)于Spring?Boot請求處理之常用參數(shù)注解使用的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-03-03

最新評論

安康市| 嵊州市| 福贡县| 塔河县| 岑巩县| 卢龙县| 分宜县| 弥勒县| 漯河市| 武功县| 来凤县| 鄂州市| 积石山| 获嘉县| 松阳县| 乐平市| 余干县| 甘泉县| 双流县| 乳山市| 云霄县| 赤峰市| 海淀区| 日照市| 保亭| 库尔勒市| 中超| 娄烦县| 广东省| 阿拉善左旗| 马山县| 会东县| 平顶山市| 洪雅县| 万宁市| 民丰县| 获嘉县| 吉林省| 江油市| 晋城| 东乡县|