Java HashSet retainAll() Method

The HashSet.retainAll() method in Java is used to retain only the elements in the HashSet that are also contained in a specified collection. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. retainAll Method Syntax
  3. Examples
    • Retaining Elements from Another Collection
    • Retaining Elements from a List in a HashSet
  4. Conclusion

Introduction

The HashSet.retainAll() method is a member of the HashSet class in Java. It allows you to retain only those elements in the HashSet that are also contained in the specified collection. All other elements are removed from the HashSet.

retainAll Method Syntax

The syntax for the retainAll method is as follows:

public boolean retainAll(Collection<?> c)
  • The method takes a single parameter c of type Collection<?>, which specifies the collection containing elements to be retained in the HashSet.
  • The method returns a boolean value:
    • true if the HashSet changed as a result of the call.
    • false if the HashSet did not change (i.e., it already contained only the elements in the specified collection).

Examples

Retaining Elements from Another Collection

The retainAll method can be used to retain only the elements in the HashSet that are also contained in another HashSet.

Example

import java.util.HashSet;

public class RetainAllExample {
    public static void main(String[] args) {
        // Creating two HashSets of Strings
        HashSet<String> languages1 = new HashSet<>();
        HashSet<String> languages2 = new HashSet<>();

        // Adding elements to the first HashSet
        languages1.add("Java");
        languages1.add("Python");
        languages1.add("C");

        // Adding elements to the second HashSet
        languages2.add("Python");
        languages2.add("C++");
        languages2.add("C");

        // Retaining only the elements in languages1 that are in languages2
        boolean changed = languages1.retainAll(languages2);

        // Printing the result of retainAll and the HashSet
        System.out.println("Did languages1 change? " + changed);
        System.out.println("languages1 after retainAll: " + languages1);
    }
}

Output:

Did languages1 change? true
languages1 after retainAll: [C, Python]

Retaining Elements from a List in a HashSet

The retainAll method can also be used to retain only the elements in the HashSet that are also contained in a List.

Example

import java.util.HashSet;
import java.util.ArrayList;
import java.util.List;

public class RetainAllFromListExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> languages = new HashSet<>();

        // Adding elements to the HashSet
        languages.add("Java");
        languages.add("Python");
        languages.add("C");

        // Creating a List of Strings
        List<String> retainLanguages = new ArrayList<>();
        retainLanguages.add("Python");
        retainLanguages.add("Ruby"); // Not present in the HashSet

        // Retaining only the elements in the HashSet that are in the List
        boolean changed = languages.retainAll(retainLanguages);

        // Printing the result of retainAll and the HashSet
        System.out.println("Did languages change? " + changed);
        System.out.println("languages after retainAll: " + languages);
    }
}

Output:

Did languages change? true
languages after retainAll: [Python]

Conclusion

The HashSet.retainAll() method in Java provides a way to retain only the elements in the HashSet that are also contained in a specified collection. By understanding how to use this method, you can efficiently filter your collections to keep only the desired elements. This method is particularly useful for scenarios where you need to update a set by retaining a group of elements that are present in another collection, such as another HashSet or a List.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top