下面的示例打印1到10的整数和(7,8,9,10)
的列表public void streamCollect() {
ConnectableFlux<Integer> connect = Flux.range(1, 10)
.publish();
connect.subscribe(v -> System.out.println("1: " + v));
connect
.filter(number -> number > 6)
.collectList()
.subscribe(v -> System.out.println("4: " + v));
connect.connect();
}
结果:
1:1
1:2
1:3
1:4
1:5
1:6
1:7
1:8
1:9
1:10
4:[7,8,9,10]
下一个示例应该产生相同的结果,但是只打印出1到10但没有列表的数字。为什么呢?
public void streamCollect() {
ConnectableFlux<Integer> connect = Flux.<Integer>create(emitter -> {
Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.forEach(t -> emitter.next(t));
}).publish();
connect.subscribe(v -> System.out.println("1: " + v));
connect
.filter(number -> number > 6)
.collectList()
.subscribe(v -> System.out.println("4: " + v));
connect.connect();
}
结果:
1:1
1:2
1:3
1:4
1:5
1:6
1:7
1:8
1:9
1:10
答案 0 :(得分:1)
collectList等待你在创建lambda
中永远不会产生的onComplete信号