Convert Collection to List in Java [3 ways]

In this post, I will be sharing how to convert Collection to List in Java. There are three ways to achieve our goal:

1. Using List.copyOf() method [Easiest]

2. Using Java 8 Stream

3. Using ArrayList Constructor

Let's dive deep into the topic:

Read Also: Initialize List of String in Java

Convert Collection to List in Java

1. Using List.copyOf() method


We can use List.copyOf() method to convert Collection to List in Java. According to Oracle docs, List.copyOf() method returns the unmodifiable List containing the elements of the given Collection.

 import java.util.*;

public class ConvertCollectionToList {
    public static void main(String args[]) {
      // Here given Collection is HashSet   
      HashSet<String> hashset = new HashSet();
      // Adding elements
      hashset.add("Cap");
      hashset.add("T-Shirt");
      hashset.add("Jeans");
      hashset.add("Shoes");
      // Converting Collection to List using List.copyOf() method
      List<String> listValues = List.copyOf(hashset);
      // Showing the List 
      System.out.println("Converted Collection to the List using List.copyOf() method: " + listValues);
    }
}


Output:

Converted Collection to the List using List.copyOf() method: [Cap, T-Shirt, Jeans, Shoes]

2. Using Java 8 Stream API


We can use Java 8 Stream API to convert Collection to List as shown below in the example.

 import java.util.stream.*;
import java.util.*;

public class ConvertCollectionToList2 {
    public static void main(String args[]) {
      // Given Collection is HashSet    
      HashSet<Integer> hashset = new HashSet(); 
      // Adding elements to hashset object
      hashset.add(1234);
      hashset.add(2345);
      hashset.add(3456);
      hashset.add(4567);
      // Converting Collection to List using Java 8
      List<Integer> integerValues = hashset.stream().collect(Collectors.toList());
      // Printing the List 
      System.out.println("Converted Collection to the List using Java 8: " + integerValues);
    }
}


Output:

Converted Collection to the List using Java 8: [3456, 1234, 4567, 2345]

3. Using ArrayList Constructor


We can also use ArrayList constructor to convert Collection to List in Java.

Syntax of ArrayList constructor is:

 public ArrayList(Collection<? extends E> c)

The above ArrayList constructor constructs a List containing the elements of the specified collection.

 import java.util.*;

public class ConvertCollectionToList3 {
    public static void main(String args[]) {
      // Given Collection is HashMap    
      HashMap<String, Integer> hashmap = new HashMap(); 
      // Putting elements to hashmap object
      hashmap.put("Mango", 55);
      hashmap.put("Apple", 88);
      hashmap.put("Guava", 99);
      hashmap.put("Banana", 77);
      // Converting Collection to List using ArrayList constructor
      List<Integer> listValues = new ArrayList(hashmap.values());
      // Displaying the List 
      System.out.println("Converted Collection to the List using ArrayList constructor: " + listValues);
    }
}


Output:

Converted Collection to the List using ArrayList constructor: [99, 88, 55, 77]

That's all for today. Please mention in the comments if you know any other way of converting Collection to List in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry