🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The LinkedHashSet.containsAll() method in Java is used to check if the LinkedHashSet contains all elements of the specified collection.
Table of Contents
- Introduction
containsAllMethod Syntax- Examples
- Checking if All Elements are Present in LinkedHashSet
- Handling Collections with Non-Present Elements
- Conclusion
Introduction
The LinkedHashSet.containsAll() method is a member of the LinkedHashSet class in Java. It allows you to check if the LinkedHashSet contains all the elements from a specified collection. This method is useful for validating if a set includes a subset of elements.
containsAll() Method Syntax
The syntax for the containsAll method is as follows:
public boolean containsAll(Collection<?> c)
- The method takes a single parameter
cof typeCollection<?>, which represents the collection whose elements are to be checked for containment in theLinkedHashSet. - The method returns a boolean value:
trueif theLinkedHashSetcontains all elements of the specified collection.falseif theLinkedHashSetdoes not contain one or more elements of the specified collection.
Examples
Checking if All Elements are Present in LinkedHashSet
The containsAll method can be used to check if all elements of a specified collection are present in the LinkedHashSet.
Example
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class ContainsAllExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> animals = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
animals.add("Lion");
animals.add("Tiger");
animals.add("Elephant");
// Creating a list of animals to check for containment
ArrayList<String> animalsToCheck = new ArrayList<>();
animalsToCheck.add("Lion");
animalsToCheck.add("Tiger");
// Checking if the LinkedHashSet contains all elements of the list
boolean containsAll = animals.containsAll(animalsToCheck);
// Printing the result
System.out.println("Does the LinkedHashSet contain all elements? " + containsAll);
}
}
Output:
Does the LinkedHashSet contain all elements? true
Handling Collections with Non-Present Elements
The containsAll method returns false if the LinkedHashSet does not contain one or more elements of the specified collection.
Example
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class ContainsAllNonPresentExample {
public static void main(String[] args) {
// Creating a LinkedHashSet of Strings
LinkedHashSet<String> animals = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
animals.add("Lion");
animals.add("Tiger");
animals.add("Elephant");
// Creating a list of animals to check for containment
ArrayList<String> animalsToCheck = new ArrayList<>();
animalsToCheck.add("Lion");
animalsToCheck.add("Monkey"); // Monkey is not in the LinkedHashSet
// Checking if the LinkedHashSet contains all elements of the list
boolean containsAll = animals.containsAll(animalsToCheck);
// Printing the result
System.out.println("Does the LinkedHashSet contain all elements? " + containsAll);
}
}
Output:
Does the LinkedHashSet contain all elements? false
Conclusion
The LinkedHashSet.containsAll() method in Java provides a way to check if a LinkedHashSet contains all elements from another collection. By understanding how to use this method, you can efficiently validate the presence of multiple elements within a set. This method is useful for ensuring that a collection meets specific criteria, making it a valuable tool for collection management in your Java applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment