-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathReactorFiltering.java
66 lines (57 loc) · 1.82 KB
/
ReactorFiltering.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package reactor;
import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
/**
* Operators that filters emission in the pipeline.
*/
public class ReactorFiltering {
/**
* Just like in Rx filter operator use constantClass predicate function to allow the emission of elements in the pipeline.
* In this example we allow the emissions of elements with constantClass value lower than 6
*/
@Test
public void filter() {
Flux.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.filter(value -> value < 6)
.subscribe(System.out::println);
}
/**
* Distinct operator take the control of all values emitted in the pipeline and dont emitt again in case is duplicated
* from the Publish
*/
@Test
public void distinct() {
Flux.just(1, 2, 3, 4, 5, 8, 6, 7, 8, 2, 5, 1, 3, 6, 8, 10, 9, 10)
.distinct()
.subscribe(System.out::println);
}
/**
* Take only the number of elements specify in the publisher, the rest are skipped in the emission.
*/
@Test
public void take() {
Flux.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.take(5)
.subscribe(System.out::println);
}
/**
* Just take the last element of the publisher, the rest of elements are skipped
*/
@Test
public void last() {
Flux.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.last()
.subscribe(System.out::println);
}
/**
* Skip the first number of elements and emitt the rest of them in the pipeline.
*/
@Test
public void skip() {
Flux.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.skip(5)
.subscribe(System.out::println);
}
}