ホームページ  >  記事  >  Java  >  Springbootの組み込みツールクラスCollectionUtilsの使用方法

Springbootの組み込みツールクラスCollectionUtilsの使用方法

PHPz
PHPz転載
2023-05-16 13:19:211008ブラウズ

org.springframework.util.CollectionUtils

コレクションの判定

boolean hasUniqueObject(Collection collection)

ソースコードのコメントからList/の判定に使用します。 Set リスト/セット内の各要素が一意であるかどうか、つまり、リスト/セット内に重複する要素がないかどうか。しかし、ここで私が言いたいのは、この方法にはバグがあるため、この方法を使用しないでください。なぜですか? Spring-core-5.2.13.RELEASE.jar のソース コードは次のとおりです。12 行目を見ると、2 つのオブジェクトが等しいかどうかを比較するために != が使用されていることがわかります。 「==」と「等しい」の違いを覚えていますか? 「==」演算子は 2 つの変数の値が等しいかどうかを比較するために特別に使用され、equals() メソッドは 2 つの独立したオブジェクトの内容が同じかどうかを比較するために使用されます。したがって、コレクション内の要素が数値の場合は「==」を使用して比較できますが、通常の参照オブジェクトの場合は正しい結果が得られません。

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)

このメソッドは検証されており、安心して使用できます。リスト/セットが空かどうかを判断するために使用されます。

@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(コレクション ソース, コレクション候補)

ソース コードのコメントから、コレクション ソースに別のコレクション候補の要素が含まれているかどうかを判断するために使用されます。コレクション候補 の要素が集合ソースに完全に含まれているかどうか。

ソースコードから見ると、このメソッド内の要素間の比較は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)

配列配列内のすべての要素をリスト/セットに追加します。

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

プロパティ内のすべてのキーと値のペアをマップに追加します。

@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(オブジェクト ソース)

配列をコレクションに変換します。

rree

以上がSpringbootの組み込みツールクラスCollectionUtilsの使用方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。