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 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);
listObservable.subscribe(System.out::println);

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:

listObservable.subscribe(
  color -> System.out.print(color + "|"),
  System.out::println,
  System.out::println
);
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...