如何在Java中将Stream转换为TreeSet?
让我们首先创建一个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));
}
}
输出结果
ArmeniaAustralia
Canada
India
Poland
UK
US
以上是 如何在Java中将Stream转换为TreeSet? 的全部内容, 来源链接: utcz.com/z/356969.html