OneCompiler

Basic operations of spring reactive streams

294

Mapping data in a stream

  • We can transform the stream of data using map() of flux.
  • subscribe the data using subscribe() of flux
Flux.just(1, 2, 3, 4)
		  .log()
		  .map(i -> i * 10)
		  .subscribe(System.out::println);

Filter data in a stream

  • We can filter the stream of data using filter() of flux.
  • subscribe the data using subscribe() of flux
Flux.just(1, 2, 3, 4)
		  .log()
		  .filter(i -> i % 2 == 0)
		  .map(i -> i * 2)
		  .subscribe(System.out::println);

Count values in a data stream

  • We can count the stream of data using count() of flux.
  • subscribe the data using subscribe() of flux
  • count returns Mono<Integer>
Mono<Long> count = Flux.just(1, 2, 3, 4)
		  .log()
		  .filter(i -> i % 2 == 0)
		  .map(i -> i * 2)
		  .count();

Distinct values in a data stream

  • We can remove values in the stream of data using distinct() of flux.
  • subscribe the data using subscribe() of flux
  • distinct returns Flux<Integer>
Flux<Integer> list = Flux.just(1, 2, 3, 4)
		  .log()
		  .filter(i -> i % 2 == 0)
		  .map(i -> i * 2)
		  .distinct();

Log values of a data stream

  • We can log values in the stream of data using log() of flux.
  • subscribe the data using subscribe() of flux
Flux.just(1, 2, 3, 4)
		  .log()
		  .filter(i -> i % 2 == 0)
		  .map(i -> i * 2)
		  .subscribe(System.out::println);