How do you create a list from a set in Java?


We can create a list from a set using its constructor.

List<Integer> list = new ArrayList<Integer>(set);

Example

Following is the example showing the conversion of set to list −

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CollectionsDemo {
   public static void main(String[] args) {
      Set<Integer> set = new HashSet<>();
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      System.out.println("Set: " + set);
      List<Integer> list = new ArrayList<>(set);
      System.out.println("List: " + list);
   }
}

Output

This will produce the following result −

Set: [1, 2, 3, 4]
List: [1, 2, 3, 4]

Updated on: 10-May-2022

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements