Convert HashSet to ArrayList in Java Example

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.

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.

Output

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.

The addAll method adds all elements of the specified collection to this ArrayList object.

Output

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.

Output

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.

Output

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

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

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

Online Shopping