Java 集合 disjoint() 方法



描述

Java 集合 disjoint(Collection<?>, Collection<?>) 方法用於判斷兩個指定的集合是否沒有任何共同元素,如果有則返回 'false',否則返回 'true'。

宣告

以下是 java.util.Collections.disjoint() 方法的宣告。

public static boolean disjoint(Collection<?> c1,Collection<?> c2)

引數

  • c1 − 一個集合。

  • c2 − 另一個集合。

返回值

布林值,表示兩個集合是否不相交。

異常

NullPointerException − 如果任一集合為 null,則丟擲此異常。

比較整數列表的公共值示例

以下示例演示瞭如何使用 Java 集合 disjoint(Collection,Collection) 方法來比較整數列表。我們建立了兩個包含一些整數的列表。使用 disjoint(Collection,Collection) 方法,我們將一個列表的內容與另一個列表的內容進行比較,檢視是否存在任何公共元素,然後列印結果。

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<Integer> srclst = new ArrayList<>();
      List<Integer> destlst = new ArrayList<>();

      // populate two lists
      srclst.add(1);
      srclst.add(2);
      srclst.add(3);

      destlst.add(4);
      destlst.add(5);
      destlst.add(6);
      destlst.add(7);

      // check elements in both collections
      boolean iscommon = Collections.disjoint(srclst, destlst);

      System.out.println("No commom elements: "+iscommon);
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

No commom elements: true

比較字串列表的公共值示例

以下示例演示瞭如何使用 Java 集合 disjoint(Collection,Collection) 方法來比較字串列表。我們建立了兩個包含一些字串的列表。使用 disjoint(Collection,Collection) 方法,我們將一個列表的內容與另一個列表的內容進行比較,檢視是否存在任何公共元素,然後列印結果。

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<String> srclst = new ArrayList<>();
      List<String> destlst = new ArrayList<>();

      // populate two lists
      srclst.add("A");
      srclst.add("B");
      srclst.add("C");

      destlst.add("D");
      destlst.add("E");
      destlst.add("F");
      destlst.add("G");

      // check elements in both collections
      boolean iscommon = Collections.disjoint(srclst, destlst);

      System.out.println("No commom elements: "+iscommon);
   }    
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

No commom elements: true

比較物件列表的公共值示例

以下示例演示瞭如何使用 Java 集合 disjoint(Collection,Collection) 方法來比較學生物件列表。我們建立了兩個包含一些學生物件的列表。使用 disjoint(Collection,Collection) 方法,我們將一個列表的內容與另一個列表的內容進行比較,檢視是否存在任何公共元素,然後列印結果。

 
package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {

      // create two lists    
      List<Student> srclst = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));
      List<Student> destlst = new ArrayList<>(Arrays.asList(new Student(4, "Jene"),
         new Student(5, "Julie"), new Student(6, "Trus")));

      // check elements in both collections
      boolean iscommon = Collections.disjoint(srclst, destlst);

      System.out.println("No commom elements: "+iscommon);
   }    
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

No commom elements: true
java_util_collections.htm
廣告

© . All rights reserved.