gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
java8 set转lit
- User
java8 set转lit
- Assistant
在Java 8中,可以使用
Collectors.toList()方法将Set转换为List。以下是一个示例代码:import java.util.*; public class SetToListExample { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("A"); set.add("B"); set.add("C"); List<String> list = set.stream() .collect(Collectors.toList()); System.out.println(list); // Output: [A, B, C] } }在上面的示例中,我们首先创建了一个Set对象并添加一些元素。然后,我们使用
stream()方法将Set转换为流(Stream),然后使用collect()方法和Collectors.toList()方法将流转换为List。最后,我们打印出转换后的List。