How to Convert HashSet to ArrayList in Java

In this tutorial we will learn about how to convert HashSet to ArrayList in java. We will see different method for converting HashSet to list java . We will use parameterized Constructor and addAll() method of ArrayList for HashSet to ArrayList conversion .

Example 1 : Convert HashSet to ArrayList Using Parameterized Constructor

In this example, We created HashSet and add elements using add() method . Now we will initialize ArrayList Using Parameterized Constructor with HashSet elements .

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

public class HashSetToArraylist {
	
	public static void main(String[] args) {
		
		//create HashSet
		HashSet<String> hashSet = new HashSet<String>();
		
		//add element into HashSet using add() method
		hashSet.add("Java");
		hashSet.add("Vogue");
		hashSet.add("Welcomes");
		
		//print HashSet Element 
		System.out.println("HashSet Elements : "+ hashSet);
		
		
		// Converting HashSet to ArrayList
	    List<String> list = new ArrayList<String>(hashSet);

	    //print ArrayList elements
	    System.out.println("ArrayList Elements :"+list);
				
	}

}

Output:

HashSet Elements : [Java, Vogue, Welcomes]
ArrayList Elements :[Java, Vogue, Welcomes]

Example 2 : Convert HashSet to List Using addAll() Method

In this example we will convert hashset to arraylist using addAll() method of List . Firstly we created HashSet then add element into it using add() method . Then we will create list. Now we will add HashSet elements to list using List’s addAll() method .

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

public class HashSetToArraylist1 {
	
	public static void main(String[] args) {
		
		        //create HashSet
				HashSet<String> hashSet = new HashSet<String>();
				
				//add element into HashSet using add() method
				hashSet.add("Java");
				hashSet.add("Vogue");
				hashSet.add("Welcomes");
				
				//print HashSet Element 
				System.out.println("HashSet Elements : "+ hashSet);
				
				
				//create ArrayList
			    List<String> list = new ArrayList<String>();

			    //adding HashSet Element to Arraylist 
			    list.addAll(hashSet);
			    
			    //print ArrayList elements
			    System.out.println("ArrayList Elements :"+list);
			    
	}

}

Output:

HashSet Elements : [Java, Vogue, Welcomes]
ArrayList Elements :[Java, Vogue, Welcomes]

Example 3 : Convert HashSet to List Using add() Method

In this approach,we will convert HashSet to ArrayList using for loop . We created HashSet and add elements into it. We also created one list. Now We Iterate HashSet Using for-each loop and one by one element of HashSet will added to list using add() method . Let,s see java program for this approach .

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

public class HashSetToArraylist2 {
	
	public static void main(String[] args) {
		
		//create HashSet
		HashSet<String> hashSet = new HashSet<String>();
		
		//add element into HashSet using add() method
		hashSet.add("Java");
		hashSet.add("Vogue");
		hashSet.add("Welcomes");
		
		//print HashSet Element 
		System.out.println("HashSet Elements : "+ hashSet);
		
		   
		//create ArrayList
	    List<String> list = new ArrayList<String>();

	    //add HashSet element to ArrayList one by one using loop 
	     for (String element : hashSet) {
	    	 
	    	 //add element in to list 
		     list.add(element);
	      }
	    
	    //print ArrayList elements
	    System.out.println("ArrayList Elements :"+list);
	}

}

Output:

HashSet Elements : [Java, Vogue, Welcomes]
ArrayList Elements :[Java, Vogue, Welcomes]

In this tutorial we have learned different method for HashSet to ArrayList conversion . You can learn more java HashSet Example for practice .

Read More:                                                                                                                       A Guide to HashSet
 A Guide To ArrayList
 Java Doc – HashSet

Leave a Reply

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

44 − 36 =