溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Springboot內(nèi)置的工具類CollectionUtils怎么使用

發(fā)布時(shí)間:2022-12-17 09:28:31 來源:億速云 閱讀:141 作者:iii 欄目:開發(fā)技術(shù)

這篇“Springboot內(nèi)置的工具類CollectionUtils怎么使用”文章的知識點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Springboot內(nèi)置的工具類CollectionUtils怎么使用”文章吧。

org.springframework.util.CollectionUtils

集合的判斷

boolean hasUniqueObject(Collection collection)

從源碼注釋上看,是用于判斷 List/Set 中的每個(gè)元素是否唯一,即 List/Set 中不存在重復(fù)元素。但這里要告訴大家千萬不要用這個(gè)方法,因?yàn)檫@個(gè)方法有bug,為什么呢?下面是Spring-core-5.2.13.RELEASE.jar中的源碼,且看12行,細(xì)心的人會發(fā)現(xiàn)兩個(gè)對象之間比較是否相等用的是!=。還記得“==”和“equals”的區(qū)別嗎?“==”操作符專門用來比較兩個(gè)變量的值是否相等,equals()方法是用于比較兩個(gè)獨(dú)立對象的內(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)

從源碼的注釋上看,是用于判斷集合中是否包含某個(gè)對象。這個(gè)方法也不建議使用,因?yàn)榕c上一個(gè)方法存在相同的問題,且看源碼的第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)

這個(gè)方法已驗(yàn)證過可以放心用,用于判斷 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中是否包含另一個(gè)集合candidates的任意一個(gè)元素,即集合candidates中的元素是否完全包含于集合soruce。

從源碼這個(gè)方法中的元素之間的比較用到了“equals”方法,且調(diào)用的是集合內(nèi)對象的equals方法,因此使用這個(gè)方法想要得到正確的結(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);
    //這里可以用是因?yàn)楸容^的時(shí)候調(diào)用的是equals方法
    boolean b = CollectionUtils.containsAny(list, list2);
    Assert.isTrue(b, "list1沒有包含有l(wèi)ist2中任意一個(gè)元素");
}

集合的操作

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 中最后一個(gè)元素。

@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), "獲取集合第一個(gè)元素失敗了");
 
}

T firstElement(List list)

返回集合中第一個(gè)元素。

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

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

@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怎么使用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI