How do I convert Set into List?

The code below gives you an example of converting a Set into a List. It simply done by creating a new instance of List and pass the Set as the argument of the constructor.

package org.kodejava.example.util;

import java.util.*;

public class SetToList {
    public static void main(String[] args) {
        //
        // Create a Set and add some objects into the Set.
        //
        Set set = new HashSet();
        set.add("A");
        set.add(new Long(10));
        set.add(new Date());

        //
        // Convert the Set to a List can be done by passing the Set instance into
        // the constructor of a List implementation class such as ArrayList.
        //
        List list = new ArrayList(set);
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            System.out.println("Object = " + o);
        }
    }
}

Wayan Saryada

I am a programmer, a runner, an open water diver and currently living in the island of Bali, Indonesia. If you need help on Java programming you can hire me on Fiverr.

Leave a Comment.