Set to List in Java

Given a set (HashSet or TreeSet) of strings in Java, convert it into a list (ArrayList or LinkedList) of strings.

Input : Set hash_Set = new HashSet();
        hash_Set.add("Geeks");
        hash_Set.add("For");
Output : ArrayList or Linked List with 
         following content
         {"Geeks", "for"}

Method 1 (Simple)
We simply create an list. We traverse the given set and one by one add elements to the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate conversion of
// Set to array using simple traversal
import java.util.*;
  
class Test {
  public static void main(String[] args) {
  
    // Creating a hash set of strings
    Set<String> s = new HashSet<String>();
    s.add("Geeks");
    s.add("for");
  
    int n = s.size();
    List<String> aList = new ArrayList<String>(n);
    for (String x : s)
      aList.add(x);
  
    System.out.println("Created ArrayList is");
    for (String x : aList)
      System.out.println(x);
  
    // We can created LinkedList same way
  }
}

chevron_right


Output:

Created ArrayList is
Geeks
for

Method 2 (Using ArrayList or LinkedList Constructor)

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate conversion of
// Set to list using constructor
import java.util.*;
  
class Test {
  public static void main(String[] args) {
        
    // Creating a hash set of strings
    Set<String> s = new HashSet<String>();
    s.add("Geeks");
    s.add("for");
  
    // Creating an array list using constructor
    List<String> aList = new ArrayList<String>(s);
      
    System.out.println("Created ArrayList is");
    for (String x : aList)
      System.out.println(x);
  
    System.out.println("Created LinkedList is");
    List<String> lList = new LinkedList<String>(s);      
    for (String x : lList)
      System.out.println(x);      
  }
}

chevron_right


Output:

Created ArrayList is
Geeks
for
Created LinkedList is
Geeks
for

Method 3 (Using addAll method)

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate conversion of
// Set to array using addAll() method.
import java.util.*;
  
class Test {
  public static void main(String[] args) {
  
    // Creating a hash set of strings
    Set<String> s = new HashSet<String>();
    s.add("Geeks");
    s.add("for");
  
    List<String> aList = new ArrayList<String>();
    aList.addAll(s);
  
    System.out.println("Created ArrayList is");
    for (String x : aList)
      System.out.println(x);
        
    List<String> lList = new LinkedList<String>();
    lList.addAll(s);
  
    System.out.println("Created LinkedList is");
    for (String x : lList)
      System.out.println(x);      
  }
}

chevron_right


Output:

Created ArrayList is
Geeks
for
Created LinkedList is
Geeks
for

Method 4 (Using stream in Java)
We use stream in Java to convert given set to steam, then stream to list. This works only in Java 8 or versions after that.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate conversion of
// Set to list using stream
import java.util.*;
import java.util.stream.*;
  
class Test {
  public static void main(String[] args) {
  
    // Creating a hash set of strings
    Set<String> s = new HashSet<String>();
    s.add("Geeks");
    s.add("for");
  
    List<String> aList = s.stream().collect(Collectors.toList());
  
    for (String x : aList)
      System.out.println(x);
  }
}

chevron_right


Output:

Geeks
for


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.