In this Article we will discuss how to initialize a HashSet using a Array or a Collection.

HashSet has a Constructor that receives a Collection as an argument and initializes the new Set with the elements of passed collection object.

 public HashSet(Collection<? extends E> c)

Initializing a HashSet with an array

Suppose we need to initialize our String HashSet with following array i.e.

// Array of String Objects
String[] strArr = {"abc", "def", "ghi", "jkl"};

But array is not a Collection, hence we need to first convert this into a List i.e.
List<String> arrList = Arrays.asList(strArr);

Now we can use this List to initialize our HashSet. Check the following example to  initialize HashSet with String array i.e.
package com.thispointer.java.collections.hashsets;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;

public class Example4 {

	public static void main(String[] args) {

		// Array of String Objects
		String[] strArr = { "abc", "def", "ghi", "jkl" };

		// Create a new Set from an array
		HashSet<String> strSet = new HashSet<>(Arrays.asList(strArr));

		System.out.println(strSet);

	}

}

Output:
[abc, def, ghi, jkl]

How to initialize a HashSet with an another HashSet

It can be done using the same HashSet constructor that receives another collection as an argument and adds all its element to the HashSet.

Checkout following example,

package com.thispointer.java.collections.hashsets;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;

public class Example31 {

	public static void main(String[] args) {

		// Create a new HashSet of String objects
		HashSet<String> setOfStrs = new HashSet<>();

		// Adding elements in hashset
		setOfStrs.add("hello");
		setOfStrs.add("abc");
		setOfStrs.add("time");
		setOfStrs.add("Hi");

		System.out.println("setOfStrs = " + setOfStrs);

		// Create a new Set and initialize it with existing HashSet
		HashSet<String> newStrSet = new HashSet<>(setOfStrs);
		newStrSet.add("def");
		newStrSet.add("xyz");

		System.out.println("newStrSet = " + newStrSet);

	}

}

package com.thispointer.java.collections.hashsets;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;

public class Example31 {

	public static void main(String[] args) {

		// Create a new HashSet of String objects
		HashSet<String> setOfStrs = new HashSet<>();

		// Adding elements in hashset
		setOfStrs.add("hello");
		setOfStrs.add("abc");
		setOfStrs.add("time");
		setOfStrs.add("Hi");

		System.out.println("setOfStrs = " + setOfStrs);

		// Create a new Set and initialize it with existing HashSet
		HashSet<String> newStrSet = new HashSet<>(setOfStrs);
		newStrSet.add("def");
		newStrSet.add("xyz");

		System.out.println("newStrSet = " + newStrSet);

	}

}

Ouput:
setOfStrs = [Hi, abc, hello, time]
newStrSet = [Hi, abc, def, xyz, hello, time]

 

Join a list of 2000+ Programmers for latest Tips & Tutorials