public static <T> Collector<T,?,List<T>> toList()
public static <T> Collector<T,?,Set<T>> toSet()
上面说完了toCollection这里接着说一下toLIst和toSet这两个方法。其实这两个方法的作用toCollection都能实现,但是单独拿出来估计是这两方法比较常用所以为了使用方便就单独定义了这两个方法。还有toMap也一样,但是toMap稍微复杂一点上面已经单独拿出来说过了,忘记的可以翻回去温习一下。老规矩,先看例子:
void test43() {
Set<Integer> list = Set.of(2,5,8,9,4,20,11,43,55);
List<Integer> ls = list.stream().collect(Collectors.toList());
ls.stream().forEach(System.out::print);
System.out.println("\t\n"+ls.getClass().getTypeName());
}
运行结果:
5522045438911
java.util.ArrayList
看到没有,其实就是把Set中的元素放到ArrayList中,和toCollection(ArrayList::new)方法作用一样。
toSet方法也一样,这里就不多说了。