Java HashSet addAll() Method

The HashSet.addAll() method in Java is used to add all elements from a specified collection to the HashSet

Table of Contents

  1. Introduction
  2. addAll Method Syntax
  3. Examples
    • Basic Example
    • Real-World Use Case: Merging Sets of Usernames
  4. Conclusion

Introduction

The HashSet class in Java is part of the Java Collections Framework and implements the Set interface. A HashSet is used to store unique elements and provides constant-time performance for basic operations like add, remove, contains, and size. The addAll method is used to add all elements from a specified collection to the HashSet, ensuring that duplicates are not added.

addAll() Method Syntax

The syntax for the addAll method is as follows:

public boolean addAll(Collection<? extends E> c)
  • c: The collection containing elements to be added to the HashSet.
  • Returns: true if the HashSet changed as a result of the call (i.e., if any elements were added); false otherwise.

Examples

Basic Example

In this example, we'll use the addAll method to add elements from a List to a HashSet.

Example

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

public class HashSetAddAllExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> set = new HashSet<>();
        set.add("Java");
        set.add("Python");

        // Creating a List of Strings
        List<String> list = new ArrayList<>();
        list.add("C");
        list.add("JavaScript");

        // Adding all elements from the List to the HashSet
        set.addAll(list);

        // Printing the HashSet
        System.out.println("HashSet after addAll: " + set);
    }
}

Output:

HashSet after addAll: [Java, JavaScript, Python, C]

Real-World Use Case: Merging Sets of Usernames

In a web application, you might want to merge sets of usernames from different sources.

Example

import java.util.HashSet;

public class MergeUsernamesExample {
    public static void main(String[] args) {
        // Creating a HashSet to store usernames from source 1
        HashSet<String> usernames1 = new HashSet<>();
        usernames1.add("john_doe");
        usernames1.add("jane_smith");

        // Creating a HashSet to store usernames from source 2
        HashSet<String> usernames2 = new HashSet<>();
        usernames2.add("alice_jones");
        usernames2.add("john_doe"); // Duplicate username

        // Merging usernames from source 2 into source 1
        usernames1.addAll(usernames2);

        // Printing the merged set of usernames
        System.out.println("Merged Usernames: " + usernames1);
    }
}

Output:

Merged Usernames: [john_doe, jane_smith, alice_jones]

Example: Adding All Elements from Another Set

You can use the addAll method to combine elements from one HashSet into another.

Example

import java.util.HashSet;

public class CombineHashSetsExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> set1 = new HashSet<>();
        set1.add("Java");
        set1.add("Python");

        // Creating another HashSet of Strings
        HashSet<String> set2 = new HashSet<>();
        set2.add("C");
        set2.add("JavaScript");

        // Adding all elements from set2 to set1
        set1.addAll(set2);

        // Printing the combined HashSet
        System.out.println("Combined HashSet: " + set1);
    }
}

Output:

Combined HashSet: [Java, JavaScript, Python, C]

Example: Adding All Elements from a Collection

You can also use the addAll method to add elements from any collection, such as a List, Set, or any other collection.

Example

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

public class AddAllFromCollectionExample {
    public static void main(String[] args) {
        // Creating a HashSet of Strings
        HashSet<String> set = new HashSet<>();
        set.add("Java");
        set.add("Python");

        // Creating a List of Strings
        List<String> list = new ArrayList<>();
        list.add("C");
        list.add("JavaScript");

        // Adding all elements from the List to the HashSet
        set.addAll(list);

        // Printing the HashSet
        System.out.println("HashSet after addAll from List: " + set);
    }
}

Output:

HashSet after addAll from List: [Java, JavaScript, Python, C]

Conclusion

The HashSet.addAll() method in Java provides a way to add all elements from a specified collection to a HashSet, ensuring that duplicates are not added. This method is useful in various scenarios, such as merging sets of usernames, combining data from different sources, or adding elements from any collection. By understanding how to use this method, you can efficiently manage and manipulate sets in your Java applications.

Comments