Array to hashset – How to Merge an Array in a HashSet in Java

Hashset:

Array to hashset: A HashSet is a list of no duplicate elements. For the basic operations, this class provides constant time efficiency (add, remove, contains, and size). HashSet does not support element ordering. To retrieve elements from a HashSet, use the get() process.
The Set interface is implemented by HashSet. The Set is a series that contains no duplicates. This interface is a representation of the mathematical set abstraction.

Given a array ,the task is to merge the array in hashset.

Examples:

Input:

Hashset = { ‘hello’, ‘this’, ‘is’, ‘BtechGeeks’ }

array={ ‘Good’, ‘morning’, ‘this’, ‘is’, ‘Python’ }

Output:

stringset value before merging = [BTechGeeks, this, is, hello]
string set(hashset) and liststrings(array of strings) are merged succesfully
stringset value after merging = [BTechGeeks, this, is, hello, Good, morning, Python]

Merge an array in a Hashset

HashSet has a member function addAll() that can be used to insert a collection into a HashSet. However, this Collection must be of the same kind as HashSet or its base class.

It adds each element one by one to HashSet from the specified collection.

However, an array is not a collection.

To do so, we must first transform our array into a Collection, also known as a List. We can accomplish this by using the Arrays asList() function.

It returns a List of fixed size with random access  asList() functions primarily as an adapter for using non-collection-based data structures in Collection-based APIs.\

Using the addAll() function, we can easily merge this given List into a HashSet.

Below is the implementation:

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

class BtechGeeks {
    public static void main(String[] args)
    {
        System.out.println("GFG!");
        // Creating a new hashset of type string
        HashSet<String> stringset = new HashSet<>();

        //  Initializing the hashset with some random values
        stringset.add("hello");
        stringset.add("this");
        stringset.add("is");
        stringset.add("BTechGeeks");
        // printing the hashset values without merging with
        // array
        System.out.println(
            "stringset value before merging = "
            + stringset);

        // Creating a new array which contains strings
        String[] arrstrings
            = { "Good", "morning", "this", "is", "Python" };

        // converting the arrstrings to list i.e collection
        List<String> liststrings
            = Arrays.asList(arrstrings);

        // merging the hashset(stringset) with array
        boolean mergeresult = stringset.addAll(liststrings);
        // if the result is true then merge is successfuk
        if (mergeresult) {
            System.out.println(
                "string set(hashset) and liststrings(array of strings) are merged succesfully");
        }

        System.out.println(
            "stringset value after merging = " + stringset);
    }
}

Output:

stringset value before merging = [BTechGeeks, this, is, hello]
string set(hashset) and liststrings(array of strings) are merged succesfully
stringset value after merging = [BTechGeeks, this, is, hello, Good, morning, Python]

Related Programs: