This example shows how to convert LinkedHashSet to ArrayList in Java. This example also shows how to convert LinkedHashSet to ArrayList using constructor, Java 8 Stream, and addAll method.
How to convert LinkedHashSet to ArrayList in Java?
There are several using which we can convert LinkedHashSet to an ArrayList in Java as given below.
1. Using ArrayList constructor
The ArrayList class provides a constructor that accepts a collection object.
1 |
public ArrayList(Collection<? extends E> collection) |
It creates a new ArrayList object containing all elements of the specified collection object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; public class LinkedHashSetToArrayListExample { public static void main(String[] args) { LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); //using ArrayList constructor List<Integer> list = new ArrayList<Integer>( lhSetEvenNumbers ); System.out.println("ArrayList contains: " + list); } } |
Output
1 |
ArrayList contains: [2, 4, 6, 8, 10] |
2. Using the addAll method of the ArrayList class
We can also create an empty ArrayList object and then we can add all elements of the LinkedHashSet object using the addAll
method of the ArrayList class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); //create empty array list List<Integer> list = new ArrayList<Integer>(); //add all elements of LinkedHashSet to the array list list.addAll(lhSetEvenNumbers); System.out.println("ArrayList contains: " + list); |
Output
1 |
ArrayList contains: [2, 4, 6, 8, 10] |
3. Using Apache Commons CollectionUtils class
If you are using the Apache Commons library in your project, you can use the addAll
method of the CollectionUtils class to add all elements of the LinkedHashSet object to an empty ArrayList object as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); //create empty array list List<Integer> list = new ArrayList<Integer>(); //use the addAll method of the CollectionUtils class CollectionUtils.addAll(list, lhSetEvenNumbers); System.out.println("ArrayList contains: " + list); |
Output
1 |
ArrayList contains: [2, 4, 6, 8, 10] |
4. Using Java 8 stream
If you are using Java version 8 or later, you can use the Java stream to convert the Set to a List as given below.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedHashSet<Integer> lhSetEvenNumbers = new LinkedHashSet<Integer>(); lhSetEvenNumbers.add(2); lhSetEvenNumbers.add(4); lhSetEvenNumbers.add(6); lhSetEvenNumbers.add(8); lhSetEvenNumbers.add(10); List<Integer> list = lhSetEvenNumbers.stream().collect(Collectors.toList()); System.out.println("List contains: " + list); |
Output
1 |
List contains: [2, 4, 6, 8, 10] |
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet