Program to Convert Set to List in Java

08 Sep, 2021
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element.
The List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.
Below are methods to convert Set to List in Java.
Brute Force or Naive method: This method includes creating an empty list and add every element of the set to it.Algorithm:Get the Set to be converted.Create an empty listPush each element in the set into the listReturn the formed ListProgram:// Java Program to convert// Set to List in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create an empty list        List<T> list = new ArrayList<>();          // push each element in the set into the list        for (T t : set)            list.add(t);          // return the list        return list;    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]
With help of Constructor: Collections in Java have a constructor in which the direct set can be passed to create a List from it.Algorithm:Get the Set to be converted.Create a list by passing the set as parameter to the constructor.Return the formed ListProgram:// Java Program to convert// Set to List in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        List<T> list = new ArrayList<>(set);          // return the list        return list;    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]
Using Java 8 Stream: In Java 8, Stream can be used convert a set to a list by converting the set to a sequential Stream using Set.stream() and using a Collector to collect the input elements into a new List.Algorithm:Get the HashMap to be converted.Create stream from the SetConvert the set to list and collect itReturn the collected ListProgram:// Java Program to convert// HashMap to TreeMap in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        return set              // Create stream from the Set            .stream()              // Convert the set to list and collect it            .collect(Collectors.toList());    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]
Using Guava Library List.newArrayList(set): Lists.newArrayList(set) creates a mutable ArrayList instance containing the elements of the specified set.Algorithm:Get the Set to be converted.Create a new List using Lists.newArrayList() by passing the set as parameter to this function of Guava libraryReturn the formed ListProgram:// Java Program to convert// HashMap to TreeMap in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        return Lists.newArrayList(set);    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

Brute Force or Naive method: This method includes creating an empty list and add every element of the set to it.Algorithm:Get the Set to be converted.Create an empty listPush each element in the set into the listReturn the formed ListProgram:// Java Program to convert// Set to List in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create an empty list        List<T> list = new ArrayList<>();          // push each element in the set into the list        for (T t : set)            list.add(t);          // return the list        return list;    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

Algorithm:
Get the Set to be converted.Create an empty listPush each element in the set into the listReturn the formed List
Get the Set to be converted.
Create an empty list
Push each element in the set into the list
Return the formed List
Program:
// Java Program to convert// Set to List in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create an empty list        List<T> list = new ArrayList<>();          // push each element in the set into the list        for (T t : set)            list.add(t);          // return the list        return list;    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}
Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

With help of Constructor: Collections in Java have a constructor in which the direct set can be passed to create a List from it.Algorithm:Get the Set to be converted.Create a list by passing the set as parameter to the constructor.Return the formed ListProgram:// Java Program to convert// Set to List in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        List<T> list = new ArrayList<>(set);          // return the list        return list;    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

Algorithm:
Get the Set to be converted.Create a list by passing the set as parameter to the constructor.Return the formed List
Get the Set to be converted.
Create a list by passing the set as parameter to the constructor.
Return the formed List
Program:
// Java Program to convert// Set to List in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        List<T> list = new ArrayList<>(set);          // return the list        return list;    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}
Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

Using Java 8 Stream: In Java 8, Stream can be used convert a set to a list by converting the set to a sequential Stream using Set.stream() and using a Collector to collect the input elements into a new List.Algorithm:Get the HashMap to be converted.Create stream from the SetConvert the set to list and collect itReturn the collected ListProgram:// Java Program to convert// HashMap to TreeMap in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        return set              // Create stream from the Set            .stream()              // Convert the set to list and collect it            .collect(Collectors.toList());    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

Algorithm:
Get the HashMap to be converted.Create stream from the SetConvert the set to list and collect itReturn the collected List
Get the HashMap to be converted.
Create stream from the Set
Convert the set to list and collect it
Return the collected List
Program:
// Java Program to convert// HashMap to TreeMap in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        return set              // Create stream from the Set            .stream()              // Convert the set to list and collect it            .collect(Collectors.toList());    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}
Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

Using Guava Library List.newArrayList(set): Lists.newArrayList(set) creates a mutable ArrayList instance containing the elements of the specified set.Algorithm:Get the Set to be converted.Create a new List using Lists.newArrayList() by passing the set as parameter to this function of Guava libraryReturn the formed ListProgram:// Java Program to convert// HashMap to TreeMap in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        return Lists.newArrayList(set);    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}Output:Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

Algorithm:
Get the Set to be converted.Create a new List using Lists.newArrayList() by passing the set as parameter to this function of Guava libraryReturn the formed List
Get the Set to be converted.
Create a new List using Lists.newArrayList() by passing the set as parameter to this function of Guava library
Return the formed List
Program:
// Java Program to convert// HashMap to TreeMap in Java 8  import java.util.*;import java.util.stream.*;  class GFG {      // Generic function to convert set to list    public static <T> List<T> convertSetToList(Set<T> set)    {        // create a list from Set        return Lists.newArrayList(set);    }      public static void main(String args[])    {          // Create a Set using HashSet        Set<String> hash_Set = new HashSet<String>();          // Add elements to set        hash_Set.add("Geeks");        hash_Set.add("For");        hash_Set.add("Geeks");        hash_Set.add("Example");        hash_Set.add("Set");          // Print the Set        System.out.println("Set: " + hash_Set);          // construct a new List from Set        List<String> list = convertSetToList(hash_Set);          // Print the List        System.out.println("List: " + list);    }}
Output:
Set: [Set, Example, Geeks, For]
List: [Set, Example, Geeks, For]

kapoorsagar226
Java - util package
java-list
Java-List-Programs
java-set
Java-Set-Programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.