RxJava introduced Single<T>. How do I convert an Observable<T> to a Single<T>?

I think another answer is outdated. You should probably check the following methods.

singleOrError: Emits the one and only element, IndexOutOfBoundsException if the source is longer than 1 item or a NoSuchElementException if the source is empty.

firstOrError: Emits the first element or a NoSuchElementException if the source is empty.

lastOrError: Emits the lastelement or a NoSuchElementException if the source is empty.

elementAtOrError: Emits the indexth element or a NoSuchElementException.

More info on this page: https://github.com/ReactiveX/RxJava/wiki/What%27s-different-in-2.0


Note: This is for RxJava 1. See other answers below/above for Rx2 :)


2 new convenience methods were added to accomplish this very thing.

toSingle() converts an Observable that emits a single item into a Single that emits that item

toObservable converts a Single into an Observable that emits the item emitted by the Single and then completes

(source: http://reactivex.io/documentation/single.html)


In rxjava2 you can use Single.fromObservable().