How to convert Set to list in Java [Practical Examples]

Different ways to convert Set to List in Java

In Java, a Set is a collection that cannot contain duplicate elements. The Java. util. List is a child interface of Collection. List is an ordered collection of objects that can also store duplicate values. As the List preserves the insertion order, it allows positional access and insertion of elements.

There are several ways to convert the set to list in java as listed below.

Advertisement
  • Using looping
  • Using ArrayList Constructor
  • Using LinkedList Constructor
  • Using List.addAll() Method
  • Using List.copyOf() method
  • Using Stream API
  • Using Apache Commons Collections

 

Method-1: Using Looping

This is the simplest approach to copy the data from the Set to List in Java. However, this approach is only useful when size of data is small. Here, we fetch the content from the Set and add it to the List one at a time.

Example In this example, Set contains the city names. We are adding all the city names one by one using the for loop as shown below.

// Program to convert Set to List in Java
import java.util.*;

class Main {
    public static void main(String[] args) {

        // Creating a Set of strings
        Set < String > s = new HashSet < String > ();
        s.add("Delhi");
        s.add("Mumbai");
        s.add("Banglore");
        s.add("Chennai");

        int n = s.size();
        // Creating an empty List
        List < String > l = new ArrayList < String > (n);

        // Adding data one by one
        for (String x: s)
            l.add(x);

        // Printing the List
        System.out.println("Newly created List is ");
        for (String x: l)
            System.out.println(x);

    }
}

Output

Newly created List is 
Delhi
Banglore
Chennai
Mumbai

 

Method-2: Using ArrayList Constructor

This is a very simple approach to convert a Set to List in Java. Here we simply pass Set as an argument to the ArrayList while creating the list. This in turn calls the constructor of ArrayList class to initialize the data.

Example In this example, Set contains the city names. We are adding all the city names using the ArrayList constructor as shown below.

// Program to convert Set to List in Java
import java.util.*;

class Main {
    public static void main(String[] args) {

        // Creating a Set of strings
        Set < String > s = new HashSet < String > ();
        s.add("Delhi");
        s.add("Mumbai");
        s.add("Banglore");
        s.add("Chennai");

        // Passing set as an argument to Arraylist constructor
        List < String > l = new ArrayList < String > (s);

        // Printing the List
        System.out.println("Newly created List is ");
        for (String x: l)
            System.out.println(x);
    }
}

Output

Advertisement
Newly created List is 
Delhi
Banglore
Chennai
Mumbai

 

Method-3: Using LinkedList Constructor

This is a very simple approach to convert a Set to List in Java. Here we simply pass Set as an argument to the LinkedList while creating the list. This in turn calls the constructor of LinkedList class to initialize the data.

Example In this example, Set contains the city names. We are adding all the city names using the LinkedList constructor as shown below.

// Program to convert Set to List in Java
import java.util.*;

class Main {
    public static void main(String[] args) {

        // Creating a Set of strings
        Set < String > s = new HashSet < String > ();
        s.add("Delhi");
        s.add("Mumbai");
        s.add("Banglore");
        s.add("Chennai");

        // Passing set as an argument to LinkedList constructor
        List < String > l = new LinkedList < String > (s);

        // Printing the List
        System.out.println("Newly created List is ");
        for (String x: l)
            System.out.println(x);
    }
}

Output

Newly created List is 
Delhi
Banglore
Chennai
Mumbai

 

Method-4: Using List.addAll() Method

In this approach, we are using addAll() method of List Interface. The addAll() method appends all the elements in the specified collection to the end of the list. In this way we can add all the elements to the ArrayList and LinkedList.

Example In this example, Set contains the city names. We are adding all the city names using the addall() method to ArrayList and LinkedList as shown below.

// Program to convert Set to List in Java
import java.util.*;

class Main {
    public static void main(String[] args) {

        // Creating a Set of strings
        Set < String > s = new HashSet < String > ();
        s.add("Delhi");
        s.add("Mumbai");
        s.add("Banglore");
        s.add("Chennai");

        // Creating linkedlist
        List < String > l = new LinkedList < String > ();

        // Adding the elements to the list
        l.addAll(s);

        // Printing the List
        System.out.println("Newly created LinkedList is ");
        for (String x: l)
            System.out.println(x);

        // Creating ArrayList
        List < String > l1 = new ArrayList < String > ();

        // Adding the elements to the list
        l1.addAll(s);

        // Printing the List
        System.out.println("Newly created ArrayList is ");
        for (String x: l1)
            System.out.println(x);
    }
}

Output

Advertisement
Newly created LinkedList is 
Delhi
Banglore
Chennai
Mumbai
Newly created ArrayList is 
Delhi
Banglore
Chennai
Mumbai

 

Methd-5: Using List.copyOf() Method

This approach is supported from Java 10 onwards. The copyOf is a static method that provides a convenient way to create immutable lists. The list created by this method cannot be modified. Moreover, any call to modify the list will raise an UnsupportedOperationException. Also, the list created here will not allow null elements.

Example In this example, Set contains the city names. We are adding all the city names using the copyOf() method to ArrayList and LinkedList as shown below.

// Program to convert Set to List in Java
import java.util.*;
class Main {
    public static void main(String[] args) {

        // Creating a Set of strings
        Set < String > s = new HashSet < String > ();
        s.add("Delhi");
        s.add("Mumbai");
        s.add("Banglore");
        s.add("Chennai");

        // Creating linkedlist
        List < String > l = new LinkedList < String > ();

        // Adding the elements to the list
        l = List.copyOf(s);

        // Printing the List
        System.out.println("Newly created LinkedList is ");
        for (String x: l)
            System.out.println(x);

        // Creating ArrayList
        List < String > l1 = new ArrayList < String > ();

        // Adding the elements to the list
        l1 = List.copyOf(s);

        // Printing the List
        System.out.println("Newly created ArrayList is ");
        for (String x: l1)
            System.out.println(x);
    }
}

Output

Newly created LinkedList is 
Delhi
Banglore
Chennai
Mumbai
Newly created ArrayList is 
Delhi
Banglore
Chennai
Mumbai

 

Method-6: Using Stream API

This approach can be used from Java 8 onwards to convert set to list in Java. Here we are converting set to stream and then that stream is converted to the list. Here, toList() method collects all the stream elements into a List.

Example In this example, Set contains the city names. We are adding all the city names to the stream and then to the list as shown below.

// Program to convert Set to List in Java
import java.util.*;
import java.util.stream.Collectors;

class Main {
    public static void main(String[] args) {

        // Creating a Set of strings
        Set < String > s = new HashSet < String > ();
        s.add("Delhi");
        s.add("Mumbai");
        s.add("Banglore");
        s.add("Chennai");

        // Creating linkedlist
        // Adding the elements to the list
        List < String > l = s.stream().collect(Collectors.toList());

        // Printing the List
        System.out.println("Newly created LinkedList is ");
        for (String x: l)
            System.out.println(x);
    }
}

Output

Advertisement
Newly created LinkedList is 
Delhi
Banglore
Chennai
Mumbai

 

Method-7: Using Apache Commons Collections

In this approach, we have to add apache.commons.collections4 jar. Thereafter, we have to add this jar to our existing project folder using the below given steps.

  1. Right click your project folder in eclipse
  2. Click on Build Path -> Add external archives
  3. Select the jar (commons-collections4-4.4.jar)from the extracted zip folder.
  4. You will see that jar added to Referenced Libraries

Example In this example we are using addAll() method of CollectionUtils Class to copy the content of the set to list.

// Program to Convert Set to List in Java
import org.apache.commons.collections4.CollectionUtils;
import java.util.*;
public class Main {

    public static void main(String[] args) {

        Set < String > s = new HashSet < String > ();
        s.add("Delhi");
        s.add("Mumbai");
        s.add("Banglore");
        s.add("Chennai");
        List < String > l = new ArrayList < String > (4);
        CollectionUtils.addAll(l, s);

        System.out.println("Newly created LinkedList is ");
        for (String x: l)
            System.out.println(x);

    }
}

Output

Newly created LinkedList is 
Delhi
Banglore
Chennai
Mumbai

 

Summary

The knowledge of copy the set to list in Java is very useful while working on a real time applications. It’s important to note to the type of list that is created from each method. As the copyOf() method produces an immutable list and can’t handle null elements. In this tutorial, we covered seven different approaches to copy the data. As per the requirement of an application, we can choose an appropriate approach for copying. We learned in detail about this approaches with an example. All in all, this tutorial, covers everything that you need to know in order to have a clear view on copying data from set to list in Java.

 

References

List Class
CollectionUtils Class

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X