The LinkedHashSet.containsAll(Collection<?> c)
method in Java is used to check if the LinkedHashSet
contains all elements of the specified collection.
Table of Contents
- Introduction
containsAll
Method Syntax- Examples
- Checking if All Elements are Present in LinkedHashSet
- Handling Collections with Non-Present Elements
- Real-World Use Case
- Use Case: Task Dependency Check
- Conclusion
Introduction
The LinkedHashSet.containsAll(Collection<?> c)
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
c
of typeCollection<?>
, which represents the collection whose elements are to be checked for containment in theLinkedHashSet
. - The method returns a boolean value:
true
if theLinkedHashSet
contains all elements of the specified collection.false
if theLinkedHashSet
does 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
Real-World Use Case
Use Case: Task Dependency Check
In a task management system, certain tasks may depend on the completion of other tasks. The containsAll
method can be used to check if all prerequisite tasks are completed before starting a new task.
Example
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class TaskDependencyCheck {
public static void main(String[] args) {
// Creating a LinkedHashSet to store completed tasks
LinkedHashSet<String> completedTasks = new LinkedHashSet<>();
// Adding completed tasks
completedTasks.add("Design phase");
completedTasks.add("Development phase");
completedTasks.add("Testing phase");
// Creating a list of prerequisite tasks for deployment
ArrayList<String> prerequisiteTasks = new ArrayList<>();
prerequisiteTasks.add("Design phase");
prerequisiteTasks.add("Development phase");
prerequisiteTasks.add("Testing phase");
prerequisiteTasks.add("Code review");
// Checking if all prerequisite tasks are completed
boolean canDeploy = completedTasks.containsAll(prerequisiteTasks);
// Printing the result
if (canDeploy) {
System.out.println("All prerequisite tasks are completed. Ready for deployment.");
} else {
System.out.println("Prerequisite tasks are not completed. Deployment not possible.");
}
}
}
Output:
Prerequisite tasks are not completed. Deployment not possible.
Conclusion
The LinkedHashSet.containsAll(Collection<?> c)
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. The real-world use case of a task dependency check illustrates the practical application of this method in validating task prerequisites.