How to Check If There Is No Common Element In Two List Objects in Java


In this example, let’s us see how to determine if there is no common element between 2 objects in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class DisjointExample {
 
    public static void main(String a[]){
         
        List list1 = new ArrayList();
        list1.add("java");
        list1.add("c++");
        list1.add("python");
        List list2 = new ArrayList();
        list2.add("c");
        list2.add("c++");
        list2.add("java");
        //Collections.disjoint() method returns true if the two collections have no elements in common. 
        boolean isCommon = Collections.disjoint(list1, list2);
        System.out.println("Is there no common element between two list? " + isCommon);
    }
}

Output:

Is there no common element between two list? false

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments