Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.util.Collections.disjoint() Method in java with Examples
Next article icon

Java.util.Collections.disjoint() Method in java with Examples

Last Updated : 07 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
java.util.Collections.disjoint() method is present in java.util.Collections class. It is used to check whether two specified collections are disjoint or not. More formally, two collections are disjoint if they have no elements in common.
  Syntax:  public static boolean disjoint(Collection<?> c1, Collection<?> c2)  Parameters :   c1 - a collection  c2 - a collection  Returns :  true if the two specified collections have no elements in common.  Throws:  NullPointerException - if either collection is null.  NullPointerException - if one collection contains a null element and null is not an eligible   element for the other collection.  ClassCastException - if one collection contains an element that is of a type which is ineligible   for the other collection.  
Note that it is permissible to pass the same collection in both parameters, in which case the method will return true if and only if the collection is empty. Java
// Java program to demonstrate working of  // java.utils.Collections.disjoint()  import java.util.*;   public class DisjointDemo {     public static void main(String[] args)     {         // Let us create array list of strings         List<String>  mylist1 = new ArrayList<String>();         mylist1.add("practice");         mylist1.add("code");         mylist1.add("quiz");         mylist1.add("geeksforgeeks");                  // Let us create vector of strings         List<String>  mylist2 = new Vector<String>();         mylist2.add("geeks");         mylist2.add("geek");         mylist2.add("for");         mylist2.add("coder");                  // Let us create a vector          List mylist3 = new Vector();                  mylist3.add(1);          mylist3.add("practice");                       // Let us create a Set of strings         Set<String>  mylist4 = new HashSet<String>();         mylist4.add("practice");         mylist4.add("code");         mylist4.add("quiz");         mylist4.add("geeksforgeeks");                           // Here we are using disjoint() method to check          // whether two collections are disjoint or not         System.out.println("is mylist1 disjoint to mylist2 : " +                             Collections.disjoint(mylist1, mylist2));                  System.out.println("is mylist1 disjoint to mylist3 : " +                             Collections.disjoint(mylist1, mylist3));                  System.out.println("is mylist1 disjoint to mylist4 : " +                             Collections.disjoint(mylist1, mylist4));      } } 
Output:
  is mylist1 disjoint to mylist2 : true  is mylist1 disjoint to mylist3 : false  is mylist1 disjoint to mylist4 : false  

How to quickly check whether two arrays in Java are disjoint or not?

Arrays class in Java doesn’t have disjoint method. We can use Collections.disjoint() to check quickly disjoincy of two arrays. Java
// Java program to demonstrate disjoint  // method with arrays  import java.util.*;   public class DisjointDemo {     public static void main(String[] args)     {         // Let us create  arrays of integers         Integer arr1[] = {10, 20, 30, 40, 50};         Integer arr2[] = {60, 70, 80, 90, 100};         Integer arr3[] = {50, 70, 80, 90, 100};         Double  arr4[] = {50.0, 60.0, 110.0};                           // Please refer below post for details of asList()         // https://www.geeksforgeeks.org/array-class-in-java/         // Here we are using disjoint() method to check          // whether two arrays are disjoint or not         System.out.println("is arr1 disjoint to arr2 : " +                          Collections.disjoint(Arrays.asList(arr1), Arrays.asList(arr2)));                  System.out.println("is arr1 disjoint to arr3 : " +                          Collections.disjoint(Arrays.asList(arr1), Arrays.asList(arr3)));                  System.out.println("is arr1 disjoint to arr4 : " +                          Collections.disjoint(Arrays.asList(arr1), Arrays.asList(arr4)));      } } 
Output:
  is arr1 disjoint to arr2 : true  is arr1 disjoint to arr3 : false  is arr1 disjoint to arr4 : true  

Next Article
Java.util.Collections.disjoint() Method in java with Examples

G

gaurav miglani
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

    Collection contains() method in Java with Examples
    The contains(Object element) of java.util.Collection interface is used to check whether the element 'element' exists in this collection. This method returns a boolean value depicting the presence of the element. If the element is present, it returns true, else it returns false. Syntax: Collection.co
    3 min read
    Collections list() method in Java with Examples
    The list() method of java.util.Collections class is used to return an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration. This method provides interoperability between legacy APIs that return enumerations and new APIs that requi
    2 min read
    Collectors toList() method in Java with Examples
    The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c
    2 min read
    Collections.singleton() method in Java with example
    java.util.Collections.singleton() method is a java.util.Collections class method. It creates a immutable set over a single specified element. An application of this method is to remove an element from Collections like List and Set. Syntax: public static Set singleton(T obj) and public static List si
    3 min read
    Collection add() Method in Java with Examples
    The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences