The code below gives you an example of converting a java.util.Set
into a java.util.List
. It has simply done by creating a new instance of List
and pass the Set
as the argument of the constructor.
package org.kodejava.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);
}
}
}
The output of the code snippet are:
Object = A
Object = 10
Object = Mon Oct 04 20:20:26 CST 2021
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024