Unwrapping lists

However, the resulting list is not something we really want to work with--the goal is to get to the YahooStockQuote object, which actually contains the data. That can be achieved with a .flatMap() call:

.toObservable()
.map(r -> r.getQuery().getResults().getQuote())
.flatMap(r -> Observable.fromIterable(r))

Here, we create a new Observable from the resulting list and, using .flatMap(), we feed the result of the just created Observable into the original Observable. This might be confusing but, basically, we've transformed one item of the List<YahooStockQuote> type into many items of the YahooStockQuote type. The .flatMap() operator will be covered more in the later chapters.

Instead of r -> Observable.fromIterable(r), we can use a method reference--Observable::fromIterable--inside the .flatMap(). There is basically no difference, and sometimes we will use the lambda version to make the action executed more explicit.

Also, we've used a .toObservable() call in the beginning because we needed to convert an Observable of the Single type into actual Observable to match the type of Observable.fromIterable(). We will cover these things in more detail in the later chapters, so do not stress if they do not make perfect sense just yet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.37.12