Java LinkedHashSet removeAll() Method

The LinkedHashSet.removeAll() method in Java is used to remove all the elements from the LinkedHashSet that are also contained in the specified collection.

Table of Contents

  1. Introduction
  2. removeAll Method Syntax
  3. Examples
    • Removing Common Elements from LinkedHashSet
    • Handling Collections with Non-Present Elements
  4. Conclusion

Introduction

The LinkedHashSet.removeAll() method is a member of the LinkedHashSet class in Java. It allows you to remove all the elements from the LinkedHashSet that are also contained in the specified collection. This method is useful for finding the difference between two collections.

removeAll() Method Syntax

The syntax for the removeAll method is as follows:

public boolean removeAll(Collection<?> c)
  • The method takes a single parameter c of type Collection<?>, which represents the collection containing elements to be removed from the LinkedHashSet.
  • The method returns a boolean value:
    • true if the LinkedHashSet changed as a result of the call.
    • false if the LinkedHashSet did not change (i.e., none of the elements in the specified collection were present in the LinkedHashSet).

Examples

Removing Common Elements from LinkedHashSet

The removeAll method can be used to remove all elements from the LinkedHashSet that are also in the specified collection.

Example

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class RemoveAllExample {
    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 be removed
        ArrayList<String> animalsToRemove = new ArrayList<>();
        animalsToRemove.add("Lion");
        animalsToRemove.add("Giraffe");

        // Removing all elements that are in the list
        boolean isChanged = animals.removeAll(animalsToRemove);

        // Printing the result of removeAll and the LinkedHashSet
        System.out.println("Was the LinkedHashSet changed? " + isChanged);
        System.out.println("LinkedHashSet after removeAll: " + animals);
    }
}

Output:

Was the LinkedHashSet changed? true
LinkedHashSet after removeAll: [Tiger, Elephant]

Handling Collections with Non-Present Elements

If the specified collection contains elements that are not present in the LinkedHashSet, the removeAll method will have no effect on those elements.

Example

import java.util.ArrayList;
import java.util.LinkedHashSet;

public class RemoveAllNonPresentExample {
    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 be removed with non-present elements
        ArrayList<String> animalsToRemove = new ArrayList<>();
        animalsToRemove.add("Giraffe");
        animalsToRemove.add("Monkey");

        // Removing all elements that are in the list
        boolean isChanged = animals.removeAll(animalsToRemove);

        // Printing the result of removeAll and the LinkedHashSet
        System.out.println("Was the LinkedHashSet changed? " + isChanged);
        System.out.println("LinkedHashSet after removeAll: " + animals);
    }
}

Output:

Was the LinkedHashSet changed? false
LinkedHashSet after removeAll: [Lion, Tiger, Elephant]

Conclusion

The LinkedHashSet.removeAll() method in Java provides a way to remove all elements from a LinkedHashSet that are contained in another collection. By understanding how to use this method, you can efficiently find the difference between two collections and manage the elements in your sets. This method is useful for filtering out specific elements from a collection, making it a valuable tool for collection management in your Java applications.

Comments