Home  >  Core Java

Collections.disjoint in Java

By Arvind Rai, February 04, 2022
Find the Java doc of Collections.disjoint method.
static boolean disjoint(Collection<?> c1, Collection<?> c2) 
1. The disjoint method returns true if the two specified collections have no elements in common.
2. The method returns false if at least one element is common in both the collection.
3. The method throws NullPointerException if any of the collection is null.



Example-1

CollectionsDisjoint1.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsDisjoint1 {
  public static void main(String[] args) {
    List<String> list1 = new ArrayList<>(); 
    list1.add("A");
    list1.add("B");
    list1.add("C");
    
    List<String> list2 = new ArrayList<>(); 
    list2.add("C");
    list2.add("D");
    list2.add("E");
    
    boolean res1 = Collections.disjoint(list1, list2);
    System.out.println(res1); // Output: false
    
    List<String> list3 = new ArrayList<>(); 
    list3.add("F");
    list3.add("G");

    boolean res2 = Collections.disjoint(list1, list3);
    System.out.println(res2); // Output: true    
  }
} 

Example-2

CollectionsDisjoint2.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsDisjoint2 {
  public static void main(String[] args) {
    List<Integer> list1 = new ArrayList<>(); 
    list1.add(10);
    list1.add(20);
    list1.add(30);
    
    List<Integer> list2 = new ArrayList<>(); 
    list2.add(30);
    list2.add(40);
    list2.add(50);
    
    boolean res1 = Collections.disjoint(list1, list2);
    System.out.println(res1); // Output: false
    
    List<Integer> list3 = new ArrayList<>(); 
    list3.add(60);
    list3.add(70);

    boolean res2 = Collections.disjoint(list1, list3);
    System.out.println(res2); // Output: true    
  }
} 

Example-3

CollectionsDisjoint3.java
package com.concretepage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionsDisjoint3 {
  public static void main(String[] args) {
	Person a1 = new Person("A");
	Person a2 = new Person("B");
	Person a3 = new Person("C");
	Person a4 = new Person("D");
	Person a5 = new Person("E");
	Person a6 = new Person("F");
	List<Person> list1 = new ArrayList<Person>();
	list1.add(a1);
	list1.add(a2);
	list1.add(a3);
	List<Person> list2 = new ArrayList<Person>();
	list2.add(a4);
	list2.add(a5);
	list2.add(a6);
	System.out.println(Collections.disjoint(list1, list2)); // Output: true
	// Now add a common value
	Person a7 = new Person("B");
	list2.add(a7);
	System.out.println(Collections.disjoint(list1, list2)); // Output: false
  }
}

class Person {
  private String name;

  public Person(String name) {
	this.name = name;
  }

  @Override
  public boolean equals(Object o) {
	return name.equals(((Person) o).name);
  }

  public String getName() {
	return name;
  }
} 

Reference

Java Collections
POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us