Home » Java programming language

Java Collections disjoint() Method with Example

Collections Class disjoint() method: Here, we are going to learn about the disjoint() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 03, 2020

Collections Class disjoint() method

  • disjoint() method is available in java.util package.
  • disjoint() method is used to check whether the given Collection objects may contain any common elements or not.
  • disjoint() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • disjoint() method may throw an exception at the time of checking no common element exists.
    NullPointerException: This exception may throw when the given parameter is null exists.

Syntax:

    public static boolean disjoint(Collection cl1, Collection cl2);

Parameter(s):

  • Collection cl1, Collection cl2 – represent the different Collection objects.

Return value:

The return type of this method is boolean, it returns true when no common element exists in Collection object otherwise it returns false.

Example:

// Java program is to demonstrate the example
// of boolean disjoint() of Collections

import java.util.*;
public class Disjoint {
    public static void main(String args[]) {
        // Instantiate two LinkedList object
        List < Integer > l1 = new LinkedList < Integer > ();
        List < Integer > l2 = new LinkedList < Integer > ();

        // By using add() method is to add
        // few elements in l1
        l1.add(10);
        l1.add(20);
        l1.add(30);
        l1.add(40);

        // By using add() method is to add
        // few elements in l2
        l2.add(60);
        l2.add(70);
        l2.add(80);
        l2.add(90);

        // Display LinkedList
        System.out.println("l1: " + l1);
        System.out.println("l2: " + l2);

        // By using disjoint() method returns 
        // true when no common elements exists
        // in both the collections
        boolean status = Collections.disjoint(l1, l2);

        System.out.println();

        // Display status
        System.out.println("Collections.disjoint(l1,l2): " + status);
    }
}

Output

l1: [10, 20, 30, 40]
l2: [60, 70, 80, 90]

Collections.disjoint(l1,l2): true





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.