Given a set (HashSet or TreeSet) of strings in Java, convert it into a list (ArrayList or LinkedList) of strings. In java, Set interface is present in java.util package and extends the Collection interface is an unordered collection of objects in which duplicate values cannot be stored. List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored.
Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : ArrayList or Linked List with following content {"Geeks", "for"}
Approaches to Convert a Set to a List in Java
There are numerous approaches to do the conversion of a Set to List in Java. A few of them are listed below.
- Using Set Traversing
- Using ArrayList or LinkedList Constructor
- Using addAll() method
- Using Stream in Java
- Using List.copyOf() in java
1. Using Set Traversing
We simply create a list. We traverse the given set and one by one add elements to the list.
Java
// 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 create LinkedList same way } } |
Created ArrayList is Geeks for
2. Using ArrayList or LinkedList Constructor
We can simply convert a Set into a List using the constructor of an ArrayList or LinkedList.
Java
// 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); } } |
Created ArrayList is Geeks for Created LinkedList is Geeks for
3. Using addAll() Method
The Set to List conversion in Java can also be performed using the addAll() method of Java List.
Java
// 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); } } |
Created ArrayList is Geeks for Created LinkedList is Geeks for
4. Using Stream in Java
We use the stream in Java to convert the given set to steam, then stream to list. This works only in Java 8 or versions after that.
Java
// 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); } } |
Geeks for
5. Using List.copyOf() Method
We can convert the set to list by using List.copyOf(object) in java. For this, we have to import the package java.util.List.* It is a static factory method which creates the list object from another collection or object.
Java
import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { Set<String> s = new HashSet<String>(); s.add( "geeks" ); s.add( "forgeeks" ); s.add( "learning" ); s.add( "platform" ); List<String> aList = new ArrayList<>(); aList = List.copyOf(s); System.out.println( "Created ArrayList is :" + aList); } } |
Created ArrayList is :[geeks, forgeeks, learning, platform]