让我们首先创建一个Stream:
Stream<String> stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");
现在将Stream转换为TreeSet:
Set<String> set = stream.collect(Collectors.toCollection(TreeSet::new));
以下是在Java中将String转换为TreeSet的示例:
import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream<String> stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland"); Set<String> set = stream.collect(Collectors.toCollection(TreeSet::new)); set.forEach(val -> System.out.println(val)); } }
输出结果
Armenia Australia Canada India Poland UK US