Tutorials  /  JavaScript

How to Convert Set to List in Java

Ccentron Redaktion · January 2025 ·3 min read ·JavaScript, Tutorial

Introduction to Convert Set to List

Lists in Java are ordered collections of data, whereas sets are unordered collections of data. A list can have duplicate entries, but a set cannot. Both data structures are useful in different scenarios.

Knowing how to convert a set into a list is useful as it transforms unordered data into ordered data.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Initializing a Set

Let’s initialize a set and add some elements to it.

Python
import java.util.*;

public class Main {

    public static void main(String[] args)
    {
        Set<Integer> a = new HashSet<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(1);
        System.out.println(a);
    }
}

The add() method of HashSet adds elements to the set. Note that the elements are distinct. Sets are unordered, so there’s no way to get elements by their insertion order.

Convert Set to List in Java

Let’s convert the set into a list. There are multiple ways of doing so, each with subtle differences.

1. List Constructor with Set Argument

The most straightforward way is by passing the set as an argument while creating the list.

Python
import java.util.*;

public class Main {

    public static void main(String[] args)
    {
        Set<Integer> a = new HashSet<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(1);

        List<Integer> arr = new ArrayList<>(a);
        System.out.println(arr);
        System.out.println(arr.get(1));
    }
}

Since the set has been converted to a list, the elements are now ordered. This means we can use the get() method to access elements by index.

2. Using Conventional For Loop

We can use a conventional for loop to explicitly copy the elements from the set to the list.

Python
import java.util.*;

public class Main {

    public static void main(String[] args)
    {
        Set<Integer> a = new HashSet<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(1);

        List<Integer> arr = new ArrayList<>();
        for (int i : a)
            arr.add(i);
        System.out.println(arr);
        System.out.println(arr.get(1));
    }
}

The for loop iterates over the set and adds each element to the list.

3. List addAll() Method

The addAll() method of List adds multiple values to the list at once. It also works for adding elements of a set to a list.

Python
import java.util.*;

public class Main {

    public static void main(String[] args)
    {
        Set<Integer> a = new HashSet<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(1);

        List<Integer> arr = new ArrayList<>();
        arr.addAll(a);
        System.out.println(arr);
        System.out.println(arr.get(1));
    }
}

4. Stream API collect() Method

The Stream.collect() method, available from Java 8 onwards, collects all Stream elements into a List.

Python
import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args)
    {
        Set<Integer> a = new HashSet<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(1);

        List<Integer> arr = a.stream().collect(Collectors.toList());
        System.out.println(arr);
        System.out.println(arr.get(1));
    }
}

Note that Stream.collect() does not guarantee the type or thread-safety of the returned List.

5. List.copyOf() Method

From Java 10 onwards, the copyOf() method creates an unmodifiable list from a collection.

Python
import java.util.*;

public class Main {

    public static void main(String[] args)
    {
        Set<Integer> a = new HashSet<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(1);

        List<Integer> arr = List.copyOf(a);
        System.out.println(arr);
        System.out.println(arr.get(1));
    }
}

Conclusion to Convert Set to List

We explored multiple methods to convert a set into a list in Java. Each method has its advantages and limitations. For example, copyOf() creates an immutable list that doesn’t support null values, while addAll() and constructors are reliable for general use cases. Choose the method based on your specific requirements.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren