java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to 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 com.javacoderanch.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<Object> set = new HashSet<Object>();
        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<Object> list = new ArrayList<Object>(set);
        for (int i = 0; i < list.size(); i++) {
            Object o = list.get(i);
            System.out.println("Object = " + o);