Java LinkedHashSet containsAll() Method

The LinkedHashSet.containsAll() method in Java is used to check if the LinkedHashSet contains all elements of the specified collection.

Table of Contents

  1. Introduction
  2. containsAll Method Syntax
  3. Examples
    • Checking if All Elements are Present in LinkedHashSet
    • Handling Collections with Non-Present Elements
  4. 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 c of type Collection<?>, which represents the collection whose elements are to be checked for containment in the LinkedHashSet.
  • The method returns a boolean value:
    • true if the LinkedHashSet contains all elements of the specified collection.
    • false if the LinkedHashSet 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

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.

Comments