Java Set to List
In this article, you will be acknowledged about how the process of conversion from Set or HashSet to LinkedList happens and what are the possible ways involved in conversion process.
First of all, lets know what is HashSet and LinkedList.
HashSet
A hash table that is effectively a HashMap instance serves as the foundation for the HashSet class, which implements the Set interface. The set's iteration order is not guaranteed, hence the form does not guarantee that the components will remain in the same order throughout time. The null element is permitted by this class. If the hash function distributes the elements effectively among the buckets, the class additionally offers continuous time performance for operations like add, delete, contains, and size.
LinkedList
The Collection framework found in the java.util package includes Linked List. This class is an application of LinkedList data structure, a sequential data structure where each component is a completely separate object including a data part and address portion and is not kept in a single location. Pointers and addresses are used to connect the elements. Every element is referred to as a node.
There are many methods available in Java for converting a Set to a List. We've mentioned a bunch of them below.
- Using Set Traversing
- Using ArrayList or LinkedList Constructor
- Using addAll() method
- Using Stream
- Using List.copyOf() Method
1. Set Traversing
We just construct a list. As we go over the provided set, we gradually add items to the list. Lets us understand it with a simple program.
File name: Settraverse.java
// Java Program to convert the HashSet into a LinkedList by set traversing
import java.util.*;
class Settraverse {
public static void main(String[] args)
{
// arranging a string hash set
Set<String> c = new HashSet<String>();
c.add("Take");
c.add("Risk");
int m = c.size();
List<String> cList = new ArrayList<String>(m);
for (String x : c)
cList.add(x);
System.out.println("Generated ArrayList is");
for (String x : cList)
System.out.println(x);
// Similar to manner we created linked list
}
}
Output
Generated ArrayList is
Take
Risk
2) Using ArrayList or LinkedList Constructor
Using the constructor of just an ArrayList or LinkedList, we can easily turn a Set into a List. Let us understand it with an example program
File name: Arrcons.java
// Java program to display the implementation of conversion of set to list using constructor
import java.util.*;
class Arrcons {
public static void main(String[] args)
{
// Initializing a HashSet
Set<String> f = new HashSet<String>();
f.add("Failure");
f.add("Unsucces");
// Establishing an array with the help of arraylist constructor
List<String> fList = new ArrayList<String>(f);
System.out.println("Generated ArrayList is");
for (String x : fList)
System.out.println(x);
System.out.println("Generated LinkedList is");
List<String> uList = new LinkedList<String>(f);
for (String x : uList)
System.out.println(x);
}
}
Output
Generated ArrayList is
Unsucces
Failure
Generated LinkedList is
Unsucces
Failure
3. Using addAll() Method
The addAll() method in Java List also has the ability to convert a Set to a List. Lets us understand it with an example.
File name: Addall.java
// Java program to display the implementation of conversion of set to list by addAll() import java.util.*;
class Addall{
public static void main(String[] args)
{
// Initaializing a hashset of strings
Set<String> a = new HashSet<String>();
a.add("Highly");
a.add("Educated");
List<String> sList = new ArrayList<String>();
sList.addAll(a);
System.out.println("Generated ArrayList is");
for (String x : sList)
System.out.println(x);
List<String> tList = new LinkedList<String>();
tList.addAll(a);
System.out.println("Generated LinkedList ");
for (String x : tList)
System.out.println(x);
}
}
Output
Generated ArrayList
Highly
Educated
Generated LinkedList
Highly
Educated
4. By Using Stream
The specified set is converted to a stream in Java, and the stream is then converted to a list. Only Java 8 or later versions support this. Let us understand this with an example.
File name: Bystream.java
// Java program to display the implementation of conversion of set to list by Stream in java
import java.util.*;
import java.util.stream.*;
class Bystream {
public static void main(String[] args)
{
// Insitializing hash set including strings
Set<String> b = new HashSet<String>();
b.add("Be");
b.add("Brave");
List<String> kList = b.stream().collect(Collectors.toList());
for (String x : kList)
System.out.println(x);
}
}
Output
Be
Brave
5. List.copyOf() in java
Using Java's List.copyOf(object), we may convert a set to a list. To do this, we must import the java.util.List package. The list object is created using the valid collection or object via this static factory method. Let us understand this with an example program.
File name: Listcopy.java
import java.io.*;
import java.util.*;
class Listcopy {
public static void main(String[] args)
{
Set<String> v = new HashSet<String>();
v.add("I");
v.add("He");
v.add("Him");
v.add("Them");
List<String> pList = new ArrayList<>();
pList = List.copyOf(v);
System.out.println("Generated ArrayList is :"+ pList);
}
}
Output
Generated ArrayList is :[I, Them, He, Him]