This example shows how to convert HashSet to ArrayList in Java. This example also shows how to convert Set to List using constructor, addAll method and Java 8 stream.
How to convert HashSet to an ArrayList in Java?
There are several ways using which you can convert Java Set to List as given below.
1. Using ArrayList constructor
We can use the ArrayList constructor that accepts a Collection object as an argument.
1 |
public ArrayList(Collection<? extends E> collection) |
This constructor creates a new ArrayList object having elements contained in the specified collection object. We will pass the HashSet object to this constructor to convert it to an ArrayList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.ArrayList; import java.util.HashSet; import java.util.List; public class HashSeToArrayListExample { public static void main(String[] args) { HashSet<String> hsetColors = new HashSet<String>(); hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); /* * Use ArrayList constructor and pass * the HashSet object */ List<String> aListColors = new ArrayList<String>( hsetColors ); System.out.println("ArrayList contains: " + aListColors); } } |
Output
1 |
ArrayList contains: [Red, Blue, Green] |
2. Using addAll method of the ArrayList class
If you have an existing empty ArrayList object, you can also use the addAll method of the ArrayList class.
1 |
public boolean addAll(Collection<? extends E> collection) |
The addAll
method adds all elements of the specified collection to this ArrayList object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
List<String> listColors = new ArrayList<String>(); HashSet<String> hsetColors = new HashSet<String>(); hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); /* * Use addAll method to add all elements of HashSet object * to the ArrayList object */ listColors.addAll(hsetColors); System.out.println("ArrayList contains: " + listColors); |
Output
1 |
ArrayList contains: [Red, Blue, Green] |
3. Using CollectionUtils class of Apache Commons
If you are using the Apache Commons library, you can use the addAll
method of the CollectionUtils class to convert HashSet to the ArrayList object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
List<String> listColors = new ArrayList<String>(); HashSet<String> hsetColors = new HashSet<String>(); hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); /* * Use the addAll method of the CollectionUtils class * to add all elements of HashSet to an ArrayList */ CollectionUtils.addAll(listColors, hsetColors); System.out.println("ArrayList contains: " + listColors); |
Output
1 |
ArrayList contains: [Red, Blue, Green] |
4. Using Java 8 Stream
If you are using Java version 8 or later, you can use the stream to convert Set to List as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
HashSet<String> hsetColors = new HashSet<String>(); hsetColors.add("Red"); hsetColors.add("Green"); hsetColors.add("Blue"); /* * Convert Set to List using the Java 8 stream */ List<String> listColors = hsetColors.stream().collect(Collectors.toList()); System.out.println("List contains: " + listColors); |
Output
1 |
List contains: [Red, Blue, Green] |
Please also visit convert List to Set and convert ArrayList to HashSet examples to know more.
This example is a part of the Java HashSet Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 HashSet