Book Image

Learning Reactive Programming with Java 8

By : Nickolay Tzvetinov
Book Image

Learning Reactive Programming with Java 8

By: Nickolay Tzvetinov

Overview of this book

Table of Contents (15 chapters)
Learning Reactive Programming with Java 8
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The aggregate operators and the BlockingObservable class


Aggregate operators produce the Observable instances, which emit only one item and complete. This item is composed or is computed using all the items emitted by the source Observable instance. In this section, we'll talk about only two of them. For more detailed information, refer to https://github.com/ReactiveX/RxJava/wiki/Mathematical-and-Aggregate-Operators.

The first of these operators is the count() or countLong() method. It emits the number of the items emitted by the source Observable instance. For example:

Observable
  .range(10, 100)
  .count()
  .subscribe(System.out::println);

This will print 100.

The other one is the toList() or toSortedList() method, which emits a list variable (that can be sorted) containing all of the items emitted by the source Observable instance and completes.

List<Integer> list = Observable
  .range(5, 15)
  .toList()
  .subscribe(System.out::println);

This will output the following:

[5, 6, 7, 8,...