The code below gives you an example of converting a java.util.Set
into a java.util.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<Object> set = new HashSet<>();
set.add("A");
set.add(10L);
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<>(set);
for (Object o : list) {
System.out.println("Object = " + o);
}
}
}
Latest posts by Wayan (see all)
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
- How do I get a list of all TimeZones Ids using Java 8? - April 25, 2020