O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example of GroupBy

.groupBy() is a perfect tool for this. Given a key function, the .groupBy() will emit a new set of Observables (so we will have an Observable of Observables) which will contain items that had the same value that was returned by the key function.

Let's see an example:

Observable.just(new StockUpdate("APPL", BigDecimal.ONE, new Date(), ""),new StockUpdate("GOOG", BigDecimal.ONE, new Date(), ""),new StockUpdate("APPL", BigDecimal.ONE, new Date(), ""),new StockUpdate("APPL", BigDecimal.ONE, new Date(), ""))        .groupBy(StockUpdate::getStockSymbol)        .flatMapSingle(groupedObservable ->         groupedObservable.count())        .subscribe(this::log);

This will result in the following output:

APP: main:3APP: main:1

This means three items for ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required