Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
Set hashCode() Method in Java
Next article icon

Set containsAll() Method in Java

Last Updated : 05 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The containsAll() method of Set in Java is used to check if a collection contains all the elements of a specified collection. This method is part of the Collection interface.

Example 1: This example checks if all elements of one set are present in another set and it will return true if they are identical.

Java
// Java program to demonstrates  // the working of containsAll() method import java.util.*;  class Geeks {     public static void main(String args[])     {         // Creating an empty set         Set<String> s1 = new HashSet<String>();          // Use add() method to         // add elements in the set         s1.add("Java");         s1.add("C++");         s1.add("Python");          System.out.println("Set 1: " + s1);          // Creating another empty set         Set<String> s2 = new HashSet<String>();          // Use add() method to         // add elements in the set         s2.add("Java");         s2.add("C++");         s2.add("Python");          System.out.println("Set 2: " + s2);          // Check if the set         // contains same elements         System.out.println("Does set 1 contains set 2?: "                            + s1.containsAll(s2));     } } 

Output
Set 1: [Java, C++, Python] Set 2: [Java, C++, Python] Does set 1 contains set 2?: true 

Syntax of containsAll() Method

boolean containsAll(Collection<?> c)

  • Parameter: “c” is the collection whose elements are to be checked in the current collection.
  • Return Type: This method returns “true” if the current collection contains all the elements of the specified collection, otherwise returns “false”.

Example 2: This example checks if all elements of one set are present in another set and it will return false if they are not identical.

Java
// Java program to demonstrates that  // containsALl() method returns false import java.util.*;  class Geeks {        public static void main(String args[]) {          // Creating an empty s1         Set<String> s1 = new HashSet<String>();          // Use add() method to add elements in s1         s1.add("Java");         s1.add("C++");         s1.add("Python");          System.out.println("Set 1: " + s1);          // Creating another empty s2         Set<String> s2 = new HashSet<String>();          // Use add() method to add elements in s2         s2.add("10");         s2.add("20");          System.out.println("Set 2: " + s2);          // Check if s1 contains all elements of s2         System.out.println("Does s1 contain all elements of s2: "                             + s1.containsAll(s2));     } } 

Output
Set 1: [Java, C++, Python] Set 2: [20, 10] Does s1 contain all elements of s2: false 


Next Article
Set hashCode() Method in Java
author
gopaldave
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java-Functions
  • java-set
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

  • Set in Java
    The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat
    14 min read
  • Set add() method in Java with Examples
    The add() method of Set in Java is used to add a specific element into a Set collection. The set add() function adds the element only if the specified element is not already present in the set else the function returns False if the element is already present in the Set. Declaration of add() methodbo
    2 min read
  • Set contains() method in Java with Examples
    The Java.util.Set.contains() method is used to check whether a specific element is present in the Set or not. So basically it is used to check if a Set contains any particular element. Syntax: boolean contains(Object element) Parameters: The parameter element is of the type of Set. This is the eleme
    2 min read
  • Set remove() method in Java with Examples
    The java.util.Set.remove(Object O) method is used to remove a particular element from a Set. Syntax: boolean remove(Object O) Parameters: The parameter O is of the type of element maintained by this Set and specifies the element to be removed from the Set. Return Value: This method returns True if t
    1 min read
  • Set addAll() Method in Java
    In Java, the addAll() method of the Set class is used to add all the elements of a specified collection to the current collection. The elements are added randomly without following any specific order. Example 1: This example demonstrates how to merge two TreeSet using the addAll() method. [GFGTABS]
    2 min read
  • Set clear() method in Java with Examples
    The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me
    1 min read
  • Set containsAll() Method in Java
    The containsAll() method of Set in Java is used to check if a collection contains all the elements of a specified collection. This method is part of the Collection interface. Example 1: This example checks if all elements of one set are present in another set and it will return true if they are iden
    2 min read
  • Set hashCode() Method in Java
    In Java, the hashCode() method is defined in the Object class and is used to generate a hash code for objects. It plays a very important role in hash-based collections like HashMap, HashSet, and HashTable. Example 1: This example demonstrates how hashCode() is used to get the hash code of the HashSe
    2 min read
  • Set iterator() method in Java with Examples
    The java.util.Set.iterator() method is used to return an iterator of the same elements as the set. The elements are returned in random order from what present in the set. Syntax: Iterator iterate_value = Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method i
    1 min read
  • Set removeAll() Method in Java
    In Java, the removeAll() method is part of the Collection interface. It is used to remove all elements from a collection that are present in another collection. Example 1: This example demonstrates how the removeAll() method removes all elements from the first set that are also present in the second
    2 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