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
About the Author
About the Reviewers

The Observable.from method

The Observable.from method can create an Observable instance from different Java structures. For example:

List<String> list = Arrays.asList(
  "blue", "red", "green", "yellow", "orange", "cyan", "purple"
Observable<String> listObservable = Observable.from(list);

This piece of code creates an Observable instance from a List instance. When the subscribe method is called on the Observable instance, all of the elements contained in the source list are emitted to the subscribing method. For every call to the subscribe() method, the whole collection is emitted from the beginning, element by element:

  color -> System.out.print(color + "|"),
listObservable.subscribe(color -> System.out.print(color + "/"));

This will print the colors twice with different formatting.

The true signature of this version of the from method is final static...