Java converts Set to List by using many methods like- Loop traverse, ArrayList constructor, addAll method, Java 8 Stream, Guava Library List.newArrayList(set).
There is one more easiest way to convert Set to List in Java. Both both Set and List are extended to the Collection, so converting Set into the list is quite straightforward. It only needed to just pass the value in the constructor. See the below code.
Set set = new HashSet(list);
Example: Converting ArrayList to HashSet
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SetToList
{
public static void main( String[] args )
{
System.out.println("List values .....");
List<String> list = new ArrayList<String>();
list.add("2");
list.add("1");
list.add("3");
for (String temp : list){
System.out.println(temp);
}
//Converting ArrayList to HashSet
Set<String> set = new HashSet<String>(list);
System.out.println("Set values .....");
for (String temp : set){
System.out.println(temp);
}
}
}
Output:
List values …..
2
1
3
Set values …..
1
2
3
Others ways to convert set to list in Java
- Simple Loop traverse
- ArrayList constructor
- addAll method
- Java 8 Stream
- Guava Library List.newArrayList(set).
Let’s see the examples
Simple Loop traverse
Use for-Loop or for-each loop to traverse the given set and one by one add elements to the list.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SetToList
{
public static void main( String[] args )
{
// Creating a hash set of strings
Set<String> set = new HashSet<String>();
set.add("EyeHunts");
set.add("Java");
int n = set.size();
List<String> aList = new ArrayList<String>(n);
for (String x : set)
aList.add(x);
System.out.println("Created ArrayList is");
for (String x : aList)
System.out.println(x);
}
}
Output:
Created ArrayList is
Java
EyeHunts
ArrayList constructor
Here is simply how you can use an ArrayList and LinkedList constructors.
import java.util.*;
public class SetToList
{
public static void main( String[] args )
{
// Creating a hash set
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
// Creating an array list using constructor
List<Integer> aList = new ArrayList<>(set);
System.out.println("Created ArrayList is");
for (Integer x : aList)
System.out.println(x);
System.out.println("Created LinkedList is");
List<Integer> lList = new LinkedList<>(set);
for (Integer x : lList)
System.out.println(x);
}
}
Output:
Created ArrayList is
1
2
Created LinkedList is
1
2
addAll method
Simply use lsit.addAll
method.
import java.util.*;
public class SetToList
{
public static void main( String[] args )
{
// Creating a hash set
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
List<Integer> aList = new ArrayList<>();
aList.addAll(set);
}
}
Java 8 Stream
Stream method works only in Java 8 or versions after that.
import java.util.*;
import java.util.stream.Collectors;
public class SetToList
{
public static void main( String[] args )
{
// Creating a hash set
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
List<Integer> aList = set.stream().collect(Collectors.toList());
for (Integer x : aList)
System.out.println(x);
}
}
Output: 1
2
Do comment if you knew any other way to do it or have any doubts.
Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java convert Set to List Program codes are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.