考虑以下未编译的示例:
List<Integer> list = Arrays.asList(1, 2, -3, 8);
list.stream()
.filter(x -> x > 0)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll)
.stream() // Stream<Object>
.map(x -> x * 2)
.forEach(System.out::println);
如果我更换
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll)
和
.collect(Collectors.toList())
代码将被编译。所以问题是我如何编写collect()供应商和累加器(我需要它)才能stream()在它之后调用 a ?
慕少森
相关分类