9

The high level problem I'm trying to solve is transforming a list of Foo objects contained in a fetched FooContainer (Observable) to a list of FooBar objects using RxJava.

My (confused) attempt:

fooContainerObservable
  .map(container -> container.getFooList())
  .flatMap(foo -> transformFooToFooBar(foo))
  .collect( /* What do I do here? Is collect the correct thing? Should I be using lift? */)
  .subscribe(fooBarList -> /* Display the list */);

My confusion (or at least one point of it) is the step moving the flattened list back to a list.

Note: I'm attempting to do this in an Android app.

2
  • Could you explain why you plan to use collect? If you want to collect all items to a List, just call toList. Commented Dec 9, 2014 at 2:38
  • @zsxwing because it seemed to makes sense that I was "collect"ing multiple items from the stream of events :). I wasn't aware that toList existed. If you answer suggesting that, I'll mark it as the solution! Commented Dec 9, 2014 at 16:14

1 Answer 1

17

toList can collect all items of an Observable into a List and emit it. toList can be implemented using collect, but it's much easier than collect. For your example, it will be:

fooContainerObservable
  .map(container -> container.getFooList())
  .flatMap(foo -> transformFooToFooBar(foo))
  .toList()
  .subscribe(fooBarList -> /* Display the list */);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.