Java convert Set to List

Example of a Java program which convert Set to List with ArrayList’s constructor which accept Collection.

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class MainSetToList {

    public static void main(String[] args) {
        Set<String> set = Set.of("A", "B", "C", "D");
        // Print the set
        System.out.println("Print set: " + set);

        // Use ArrayList constructor which accept Collection
        List<String> list = new ArrayList<>(set);
        // Print the list
        System.out.println("Print list: " + list);
    }
}

Output:

Print set: [D, C, B, A]
Print list: [D, C, B, A]

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.