How to Merge two HashSets in Java

Hashset:

The HashSet class implements the Set interface and is backed up by a HashMap instance that acts as a hash table. In this class, the null element is appropriate. If the Hash function correctly distributes the elements between the buckets, the class also provides continuous time for simple operations such as adding, removing, containing, and sizing.

Given two hashsets ,the task is to merge two hashsets in java

Examples:

Input:

stringset1 = [BTechGeeks, this, is, hello]
stringset2 = [Java, a, Popular, is, Programming Language]

Output:

After merging two hashsets
stringset1 = [Java, a, BTechGeeks, Popular, this, is, Programming Language, hello]

Merge two HashSets in Java

1)addAll() function

This method attaches to the end of this list all the elements in the collection in order to return it to the specified Iterator. If the specified collection is modified during the operation the behaviour of this operation is not determined (implies that the behaviour of this call is undefined if the specified collection is this list, and this list is nonempty).

Syntax:

public boolean addAll(Collection<? extends E> c)

Parameters:

 c 

 : This is the group of elements that will be added to the list.

Return:

It returns true if the specified list's elements are appended and the list changes.

2)Merging two hashsets

addAll will iterate through the given Collection one at a time, adding each element to the Hashset individually. Furthermore, if it has added any element, it will return true otherwise, it will return false.

Below is the implementation:

import java.io.*;
import java.lang.*;
import java.util.*;

class BtechGeeks {
    public static void main(String[] args)
        throws java.lang.Exception
    {

        //  creating a hashset of type string
        HashSet<String> stringset1 = new HashSet<>();

        // adding some string elements to the HashSet
        stringset1.add("hello");
        stringset1.add("this");
        stringset1.add("is");
        stringset1.add("BTechGeeks");
        // printing the elements of HashSet1
        System.out.println("stringset1 = " + stringset1);

        //  creating a hashset of type string
        HashSet<String> stringset2 = new HashSet<>();

        // adding some string elements to the HashSet
        stringset2.add("Java");
        stringset2.add("is");
        stringset2.add("a");
        stringset2.add("Popular");
        stringset2.add("Programming Language");
        // printing the elements of HashSet2
        System.out.println("stringset2 = " + stringset2);
        // merging elements of HashSet2 with HashSet1
        boolean mergeresult = stringset1.addAll(stringset2);
        // if both the hashsets are merged succesfully then
        // mergeresult is true
        if (mergeresult) {
            System.out.println(
                "Hashset2 is merged with HashSet1");
        }
        // printing the merged hashsets
        System.out.println("After merging two hashsets");
        System.out.println("setOfStrs1 = " + stringset1);
    }
}

Output:

stringset1 = [BTechGeeks, this, is, hello]
stringset2 = [Java, a, Popular, is, Programming Language]
Hashset2 is merged with HashSet1
After merging two hashsets
stringset1 = [Java, a, BTechGeeks, Popular, this, is, Programming Language, hello]

Related Programs: