为什么 collect 方法中的这个 BiConsumer Combiner 代码永远不会到达?
Why this BiConsumer Combiner code in collect method is never reached?
在 main 方法中,当我使用 collect 方法将此列表转换为 Person 列表时,我已经制作了一个具有不同 ages.Now 的列表 ages.The BiConsumer 组合器函数中的代码是从未达到。
class Person {
private int age;
public int getAge() {
return age;
}
}
//第二种收集方式
ArrayList<Integer> pAges = people.stream()
.collect(ArrayList<Integer>::new,
(listGivenBySupplier, personObjectFromPeopleStream) -> listGivenBySupplier.add(personObjectFromPeopleStream.getAge()),
(r1, r2) -> { //Also please explain what value is passed to r1 and r2
System.out.println("r1: " + r1);
System.out.println("r2: " + r2);
r1.add(2222);
r2.add(2211);
});
System.out.println("pAges:" + pAges);
仅当您并行调用流处理管道时,才会调用组合器函数。因此,将其更改为并行流,然后它应该到达组合器功能。所以这应该会触发它。
people.parallelStream()...
对于一个实例,假设有 2 个工作线程处理此工作负载。任何并行执行都涉及将源拆分为多个部分,同时执行它们,最后将部分结果合并到一个结果容器中。每个线程 T1 和 T2 都有一个关联的列表,因此容器是线程限制的。累加器函数将每个单个元素添加到关联的容器中。在线程 T1 和 T2 完成后,部分容器应合并到一个大的结果容器中。这就是组合器功能发挥作用的地方。在串行执行中,不涉及这样的结果合并,因此组合器在那里没有用处。
在 main 方法中,当我使用 collect 方法将此列表转换为 Person 列表时,我已经制作了一个具有不同 ages.Now 的列表 ages.The BiConsumer 组合器函数中的代码是从未达到。
class Person {
private int age;
public int getAge() {
return age;
}
}
//第二种收集方式
ArrayList<Integer> pAges = people.stream()
.collect(ArrayList<Integer>::new,
(listGivenBySupplier, personObjectFromPeopleStream) -> listGivenBySupplier.add(personObjectFromPeopleStream.getAge()),
(r1, r2) -> { //Also please explain what value is passed to r1 and r2
System.out.println("r1: " + r1);
System.out.println("r2: " + r2);
r1.add(2222);
r2.add(2211);
});
System.out.println("pAges:" + pAges);
仅当您并行调用流处理管道时,才会调用组合器函数。因此,将其更改为并行流,然后它应该到达组合器功能。所以这应该会触发它。
people.parallelStream()...
对于一个实例,假设有 2 个工作线程处理此工作负载。任何并行执行都涉及将源拆分为多个部分,同时执行它们,最后将部分结果合并到一个结果容器中。每个线程 T1 和 T2 都有一个关联的列表,因此容器是线程限制的。累加器函数将每个单个元素添加到关联的容器中。在线程 T1 和 T2 完成后,部分容器应合并到一个大的结果容器中。这就是组合器功能发挥作用的地方。在串行执行中,不涉及这样的结果合并,因此组合器在那里没有用处。