Java 8 Stream API 中收集器的默认 Set/List 实现是什么?

What is the default Set/List implementation with Collectors in Java 8 Stream API?

我有下面的代码快照 Java 8.

 List<Employee> employees = DataProvider.getEmployees();
 Set<Employee> set = employees.stream().filter(emp -> {
                System.out.println(emp.getName());
                return emp.getName().equals("Vishal");
            }).collect(Collectors.toSet());

我只想知道当我们使用Collectors.toSet()时默认使用Set的哪个实现(参考上面的例子)?

此外,有什么方法可以告诉 Java API 使用特定的实现(例如,HashSet)?

toSet() 收集器没有指定它使用哪个实现;你得到一个Set,仅此而已。

如果您想要特定类型的集合,请使用 toCollection() 并为您的集合提供工厂方法:

    ...collect(Collectors.toCollection(HashSet::new));