Initialize HashSet In Java

In this tutorial we will see how to initialize HashSet in java . We can create a set in java using different methods . Let’s discuss various method for java set initialization with examples .

Initialize Hashset Java With Examples

We can initialize set java using constructor , set.add() method , Collections methods and Anonymous Subclass. Now We will discuss these methods with examples .

1. Initialize HashSet java – Using Constructor

HashSet have four type constructors in java . We can create a set in java using these constructors .

HashSet<String> hashSet = new HashSet<>();
HashSet<String> hashSet = new HashSet<>(5);
HashSet<String> hashSet = new HashSet<>(5,9);
HashSet<String> hashSet = new HashSet<>(set);

In this example , we initialize HashSet java using constructor . Firstly we initialize  array of String then convert array to List and then provide this to HashSet constructor that accepts another collection . Let’s see java program for java set initialization using constructor .

import java.util.Arrays;
import java.util.HashSet;

public class HashSetInitialize {
	
public static void main(String[] args) {
	    
	   //initialize array of String 
	   String [] arr = {"JavaVogue","Welcomes","You"};   
	 
        //Constructs a new set containing the 
	    //elements in the specified collection
	   HashSet<String> hashSet = new HashSet<>(Arrays.asList(arr));
	
	   //print HashSet element 
       System.out.println("HashSet Elements : "+ hashSet);

	}

}

Output:

HashSet Elements : [JavaVogue, You, Welcomes]

2. Java Initialize HashSet Using add() Method

In this example, We will initialize hashset in java using add() method . We create HashSet by empty constructor and then add elements in this set using add() method .

import java.util.HashSet;

public class HashSetInitialize1 {
	
public static void main(String[] args) {
	
	   HashSet<String> hashSet = new HashSet<String>();
	   
	   
	   //add element in it 
	   hashSet.add("JavaVogue");
	   hashSet.add("Welcomes");
	   hashSet.add("You");
	   
	   //display the element 
	   System.out.println("HashSet Elements : "+ hashSet);
	   
	}

}

Output:

HashSet Elements : [JavaVogue, You, Welcomes]

3. Java Immutable Set Initialize

In this example we will initialize java immutable Set . Firstly we create HashSet and add element in it using add() method . Then provide this HashSet to Collections.unmodifiableSet() method to initialize java immutable Set .

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class HashSetInitialize2 {

	public static void main(String[] args) {

		HashSet<String> hashSet = new HashSet<String>();

		// add element in it
		hashSet.add("JavaVogue");
		hashSet.add("Welcomes");
		hashSet.add("You");

		// initializing Set using Collections.unmodifiableSet
		Set<String> set = Collections.unmodifiableSet(hashSet);

		System.out.println("Set Elements : " + set);

	}

}

Output:

Set Elements : [JavaVogue, You, Welcomes]

4. Java Empty Set Initialize

We will create empty set in java and initialize it using Collections.addAll() method in this example . We firstly we will initialize HashSet  (hashset) and create empty set using Collections.<String> emptySet() method . Now provides hashset to Collections.addAll() to initialize empty set in java .

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class HashSetInitialize3 {
	
	public static void main(String[] args) {
		
		HashSet<String> hashSet = new HashSet<String>();

		// add element in it
		hashSet.add("JavaVogue");
		hashSet.add("Welcomes");
		hashSet.add("You");
		
		  //create empty Set
		 Set<String> set = Collections.<String> emptySet();
		   
		 System.out.println("Set Elements : "+ set);
		   
		   //initialize Empty Set using Collections.addAll() method 
	      Collections.addAll(set = hashSet);
	        
	       System.out.println("After initialize empty Set Elements are : "+ set);
		
	}

}

Output:

Set Elements : []
After initialize empty Set Elements are : [JavaVogue, You, Welcomes]

5. Collections.singleton()

Now , If you want to create a set containing only a single element, then we can use Collections.singleton() that returns an immutable set containing that only one element. If any modification operation is performed on it , then set will throw an UnsupportedOperationException .

import java.util.Collections;
import java.util.Set;

public class HashSetInitialize4 {
	
	public static void main(String[] args) {
		
		Set<Integer> set = Collections.singleton(1);
        System.out.println("Singleton Set : "+ set);
		
	}

}

Output:

Singleton Set : [1]

6. Initialize HashSet Java Using Anonymous Subclass

We can initialize HashSet java with the help of anonymous subclass . But we should avoid this way to java set initialization because first we create anonymous class here then set , it might me create problem of memory leak.

import java.util.HashSet;

public class HashSetInitialize5 {
	
	public static void main(String[] args) {
		
		
		HashSet<String> hashSet = new HashSet<String>() {{
		    add("JavaVogue");
		    add("Welcomes");
		    add("You");
		}};
		
		System.out.println("HashSet Elements : "+ hashSet);
		
	}

}

Output:

HashSet Elements : [JavaVogue, You, Welcomes]

In this tutorial we learned about initialize HashSet java using various method with examples .Now you can do java set initialization easily . You can learn more Java HashSet program for practice.

Reference :

A Guide To HashSet

Java Doc – HashSe

Leave a Reply

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

− 1 = 4