Open In App

Convert HashSet to a ArrayList in Java

Last Updated : 04 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can be added/appended or removed from an ArrayList without the need to create a new array. 

Hashset class present in java.util package is used to create a collection that uses a hash table for storing data items. HashSet stores the elements by using a hashing mechanism.HashSet contains unique elements only and allows null value. HashSet does not maintain the insertion order and the elements are inserted on the basis of their hashcode. HashSet is the best approach for search operations. 

In order to convert a HashSet to Arraylist, we need is to use ArrayList constructor and pass the HashSet instance as a constructor argument. It will copy all elements from HashSet to the newly created ArrayList.

Java




// Java program to convert HashSet to ArrayList
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
class GFG {
    public static void main(String[] args)
    {
 
        HashSet<String> flower_set = new HashSet<>();
 
        flower_set.add("tulip");
        flower_set.add("rose");
        flower_set.add("orchid");
        flower_set.add("marie-gold");
 
        // Pass hashset to arraylist constructor
        ArrayList<String> flower_array
            = new ArrayList<>(flower_set);
 
        // all elements from hashset are copied to arraylist
        System.out.println(
            "Elements of flower Arraylist are :");
        System.out.println(flower_array);
 
        // using the get method of Arraylist to get the
        // element at index=0
        System.out.print("Element at index 0 is : "
                         + flower_array.get(0) + " ");
    }
}


Output

Elements of flower Arraylist are :
[marie-gold, rose, tulip, orchid]
Element at index 0 is : marie-gold 

 
There is another way to convert the HashSet object into ArrayList is  by using stream api introduced in Java 8. We will use the toList() method of Collectors class to convert the input data to a List but we want it to be an ArrayList so we will typecast it into ArrayList using this (ArrayList<TypeOfObject>) at front.

Java




// Java Program to convert the HashSet to ArrayList
// Using Stream Api Java 8
 
import java.io.*;
import java.util.*;
import java.util.stream.*;
class GFG {
    public static void main(String[] args)
    {
        HashSet<String> flower_set = new HashSet<>();
 
        flower_set.add("tulip");
        flower_set.add("rose");
        flower_set.add("orchid");
        flower_set.add("marie-gold");
 
        // Using stream api
        // and typecasting the List object to ArrayList
        ArrayList<String> flower_array
            = (ArrayList<String>)flower_set.stream()
                  .collect(Collectors.toList());
 
        // all elements from hashset are copied to arraylist
        System.out.println(
            "Elements of flower Arraylist are :");
        System.out.println(flower_array);
 
        // using the get method of Arraylist to get the
        // element at index=0
        System.out.print("Element at index 0 is : "
                         + flower_array.get(0) + " ");
    }
}


Output

Elements of flower Arraylist are :
[marie-gold, rose, tulip, orchid]
Element at index 0 is : marie-gold 

Another Approach : Using addAll() method

The another way to convert from HashSet to ArrayList is done by implementing the addAll() method. To use this method, we have to import the package import java.util.Collections. This will add the elements of the collections to the another collections or to the same collection which we have mentioned. It is a boolean method. It will result true, if all the elements of the collections are added successfully. Otherwise, it will return false.  

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.Collections;
import java.util.ArrayList;
import java.util.HashSet;
 
class GFG {
    public static void main (String[] args) {
      HashSet<String> set = new HashSet<>(); //Creation of HashSet
       
      set.add("GeeksforGeeks");
      set.add("learning");
      set.add("platform");
      
      ArrayList<String> list =new ArrayList<>(); //Creation of ArrayList
      list.addAll(set); //HashSet to ArrayList
       
      System.out.println("I am from HashSet "+set);
      System.out.println("I am from ArrayList "+list);
    }
}


Output

I am from HashSet [GeeksforGeeks, learning, platform]
I am from ArrayList [GeeksforGeeks, learning, platform]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads