Skip to content

Instantly share code, notes, and snippets.

@cherniag
Created August 4, 2021 14:58
Show Gist options
  • Save cherniag/c87a7584df3738890e0aa14aa183288e to your computer and use it in GitHub Desktop.
Save cherniag/c87a7584df3738890e0aa14aa183288e to your computer and use it in GitHub Desktop.
split flux into 2
public static void main(String[] args) {
Flux<Integer> integerFlux = Flux.fromIterable(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
Flux<Integer> cache = integerFlux
.doOnNext(i -> System.out.println("on next " + i))
.publish()
.refCount(2);
Flux<Integer> even = cache.filter(i -> i % 2 == 0);
Flux<Integer> odd = cache.filter(i -> i % 2 != 0);
even.subscribe(i -> System.out.println("on next even " + i), System.err::println, () -> System.out.println("complete even"));
odd.subscribe(i -> System.out.println("on next odd " + i), System.err::println, () -> System.out.println("complete odd"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment