Last active
November 13, 2019 18:00
-
-
Save abdallaadelessa/395579a59b3627f0c00394730d800586 to your computer and use it in GitHub Desktop.
My RxJava Challenge Solution https://proandroiddev.com/practical-challenges-for-rxjava-learners-1821c454de9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.reactivex.Observable; | |
import io.reactivex.Scheduler; | |
import io.reactivex.Single; | |
import io.reactivex.functions.BiFunction; | |
import io.reactivex.schedulers.Schedulers; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.FutureTask; | |
import java.util.concurrent.TimeUnit; | |
class CountriesServiceSolved implements CountriesService { | |
@Override | |
public Single<String> countryNameInCapitals(Country country) { | |
return Single.just(country) | |
.map(it -> it.name.toUpperCase()); | |
} | |
public Single<Integer> countCountries(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.count() | |
.map(Long::intValue); | |
} | |
public Observable<Long> listPopulationOfEachCountry(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.map(Country::getPopulation); | |
} | |
@Override | |
public Observable<String> listNameOfEachCountry(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.map(Country::getName); | |
} | |
@Override | |
public Observable<Country> listOnly3rdAnd4thCountry(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.skip(2) | |
.take(2); | |
} | |
@Override | |
public Single<Boolean> isAllCountriesPopulationMoreThanOneMillion(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.all(it -> it.getPopulation() > 1000000); | |
} | |
@Override | |
public Observable<Country> listPopulationMoreThanOneMillion(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.filter(it -> it.getPopulation() > 1000000); | |
} | |
@Override | |
public Observable<String> getCurrencyUsdIfNotFound(String countryName, List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.filter(it -> it.getName().equals(countryName)) | |
.map(Country::getCurrency) | |
.defaultIfEmpty("USD"); | |
} | |
@Override | |
public Observable<Long> sumPopulationOfCountries(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.map(Country::getPopulation) | |
.reduce(0L, Long::sum) | |
.toObservable(); | |
} | |
@Override | |
public Single<Map<String, Long>> mapCountriesToNamePopulation(List<Country> countries) { | |
return Observable.fromIterable(countries) | |
.toMap(Country::getName, Country::getPopulation); | |
} | |
@Override | |
public Observable<Long> sumPopulationOfCountries(Observable<Country> countryObservable1, | |
Observable<Country> countryObservable2) { | |
return countryObservable1 | |
.concatWith(countryObservable2) | |
.map(Country::getPopulation) | |
.reduce(0L, Long::sum) | |
.toObservable(); | |
} | |
@Override | |
public Observable<Country> listPopulationMoreThanOneMillionWithTimeoutFallbackToEmpty(final FutureTask<List<Country>> countriesFromNetwork) { | |
return Observable.fromFuture(countriesFromNetwork) | |
.subscribeOn(Schedulers.io()) | |
.timeout(100, TimeUnit.MILLISECONDS, Observable.empty()) | |
.flatMapIterable(it -> it) | |
.filter(it -> it.getPopulation() > 1000000); | |
} | |
@Override | |
public Single<Boolean> areEmittingSameSequences(Observable<Country> countryObservable1, | |
Observable<Country> countryObservable2) { | |
return Single.zip(countryObservable1.toList(), countryObservable2.toList(), | |
(countries, countries2) -> countries.hashCode() == countries2.hashCode()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment