I sometimes use RxJava to write in a more functional style for some complex filterings, mappings and so on (I know that this is not what it's made for, and I would be happy to use Kotlin or Java 8 for that, but I can't (I'm on Android so, stuck to 6).
However, when you try to "extract" the actual objects out of the Observable, you always need to write .toList().toBlocking().singe()
E.g. (not so complex, but you get the idea):
final List<CashRegister> cashRegisters = cashRegisterData.getCashRegisters();
return Observable.from(cashRegisters)
.map(CashRegister::getName)
.toList()
.toBlocking()
.single();
As you can see, "converting" the Observable into a list needs more lines than the actual mapping I want to perform.
Is there a way of doing this without the toList().toBlocking().single()
chain?
cashRegisters.stream().map(CashRegister::getName).collect(toList())
– JB Nizet::
and lambdas – Lovis