java 8 stream collect to linkedhashset

Java 8 中的 Stream API 提供了一种方便的收集流元素的方法,可以使用 Collectors 类的 toCollection() 和 collectingAndThen() 方法来将流收集到特定的集合中。

下面是如何使用 Java 8 Stream API 将流收集到 LinkedHashSet 中的示例代码:

List<String> list = Arrays.asList("a", "b", "c", "a", "b", "c");
LinkedHashSet<String> set = list.stream().collect(Collectors.toCollection(LinkedHashSet::new));
System.out.println(set);

输出结果:

[a, b, c]
        hellowold