首頁  >  文章  >  Java  >  Springboot內建的工具類別CollectionUtils怎麼使用

Springboot內建的工具類別CollectionUtils怎麼使用

PHPz
PHPz轉載
2023-05-16 13:19:211008瀏覽

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

@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」方法,而呼叫的是集合內物件的equals方法,因此使用這個方法想要得到正確的結果的前提是,比較的物件要重寫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);
    //这里可以用是因为比较的时候调用的是equals方法
    boolean b = CollectionUtils.containsAny(list, list2);
    Assert.isTrue(b, "list1没有包含有list2中任意一个元素");
}

集合的操作

void mergeArrayIntoCollection(Object array, Collection collection)

將陣列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, "把数据中的元素合并到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, "把数据转换成集合失败了");
}

List arrayToList(Object source)

把一個陣列轉換成一個集合。

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

以上是Springboot內建的工具類別CollectionUtils怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除