本文整理匯總了Java中io.reactivex.Observable.fromIterable方法的典型用法代碼示例。如果您正苦於以下問題:Java Observable.fromIterable方法的具體用法?Java Observable.fromIterable怎麽用?Java Observable.fromIterable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.reactivex.Observable
的用法示例。
在下文中一共展示了Observable.fromIterable方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testReactive
import io.reactivex.Observable; //導入方法依賴的package包/類
@Test
public void testReactive() {
Observable.just("Hello", "World")
.subscribe(System.out::println);
List<String> words = Arrays.asList(
"the",
"quick",
"brown",
"fox",
"jumps",
"over",
"the",
"lazy",
"dog"
);
Observable<String> wordsObs = Observable.fromIterable(words);
wordsObs.subscribe(System.out::println);
Observable<Integer> range = Observable.range(1, Integer.MAX_VALUE);
// range.subscribe(System.out::println);
wordsObs.zipWith(range, (word, row) -> String.format("%2d. %s", row, word))
.subscribe(System.out::println);
wordsObs.flatMap(word -> Observable.fromArray(word.split("")))
.sorted()
.distinct()
.zipWith(range, (word, row) -> String.format("%2d. %s", row, word))
.subscribe(System.out::println);
}
示例2: fromIterableShouldCreateAnObservableThatEmitsEachElementFromAnIterable
import io.reactivex.Observable; //導入方法依賴的package包/類
/**
* Observable.fromIterable() is another way to create an Observable. It's
* different than .just() - it is specifically designed to work with
* Collections. When just is given a collection, it converts it into an
* Observable that emits each item from the list. Let's understand how the
* two are different more clearly.
*/
@Test
public void fromIterableShouldCreateAnObservableThatEmitsEachElementFromAnIterable() {
List<String> sandwichIngredients = Arrays.asList("bread (one)", "bread (two)", "cheese", "mayo", "turkey",
"lettuce", "pickles", "jalapenos", "Sriracha sauce");
Observable<String> favoriteFoodsObservable = Observable.fromIterable(sandwichIngredients);
TestObserver<String> testObserverIteratable = null; // TODO create new TestObserver and subscribe to favoriteFoodsObservable
assertThat(testObserverIteratable.values()).hasSize(____);
assertThat(testObserverIteratable.values()).containsAll(null);
TestObserver<List<String>> testObserverForJust = new TestObserver<>();
// TODO create obseverable for sandwichIngredients with just operator and subscribe testObserverForJust to it
assertThat(testObserverForJust.values()).hasSize(1);
assertThat(testObserverForJust.values()).contains(sandwichIngredients);
/**
* ^^ As you can see here, fromIterable() & just() do very different things!
*/
}
開發者ID:vogellacompany,項目名稱:code-examples-android-expert,代碼行數:28,代碼來源:lessonA_CreatingObservableStreams.java
示例3: processStock
import io.reactivex.Observable; //導入方法依賴的package包/類
public Observable<Tuple2<String, Double>> processStock(List<String> stocks, Function<String, String> f) {
Observable<String> stockNames = Observable.fromIterable(stocks);
Observable<String> urls = stockNames.map(s -> "https://finance.google.com/finance/historical?output=csv&q=" + s);
Observable<Optional<String>> optionalObservable = urls
.subscribeOn(Schedulers.from(executorService))
.flatMap(s -> {
try {
return Observable.just(Optional.of(f.apply(s)));
} catch (Throwable t) {
return Observable.just(Optional.<String>empty());
}
})
.observeOn(Schedulers.computation())
.map(opt -> opt.map(doc -> doc.split("\n")[1].split(",")[4]))
.doOnNext(x -> System.out.println(">>>>" + x));
return Observable.zip(stockNames, optionalObservable, (name, price) ->
new Tuple2<>(name, Double.parseDouble(price.orElse("0.0"))));
}
示例4: main
import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {
List<String> items =
Arrays.asList("Alpha", "Beta", "Gamma", "Delta", "Epsilon");
Observable<String> source = Observable.fromIterable(items);
source.map(String::length).filter(i -> i >= 5)
.subscribe(s -> System.out.println("RECEIVED: " + s));
}
示例5: executeSelect
import io.reactivex.Observable; //導入方法依賴的package包/類
public <T> Observable<T> executeSelect(String query, Map<String, Object> arguments, Class<T> returnType) {
// Add in limit and offset arguments by default. The limit is increased by 1 so that we can see if there is
// more data for the client to paginate
//
// There should be a RequestContext in almost all cases. An exception would be a database call made prior
// to the request being built like the one made to authenticated the API Token
if (arguments.get(LIMIT) == null && RequestContextAccessor.get() != null) {
arguments.put(LIMIT, RequestContextAccessor.get().getLimit() + 1);
}
if (arguments.get(OFFSET) == null && RequestContextAccessor.get() != null) {
arguments.put(OFFSET, RequestContextAccessor.get().getOffset());
}
LOG.debug("Running query [{}] with values [{}]", query, arguments);
List<T> results = namedParameterJdbcTemplate.query(query, arguments, new NestedFieldRowMapper(returnType, beanMapper));
// If the results filled the limit + 1, there are more results to paginate through.
if (results.size() == RequestContextAccessor.get().getLimit() + 1) {
results.remove(results.size() - 1);
if (RequestContextAccessor.get() != null) {
RequestContextAccessor.get().setHasAnotherDbPage(Boolean.TRUE);
}
}
return Observable.fromIterable(results);
}
示例6: createEntries
import io.reactivex.Observable; //導入方法依賴的package包/類
private static Observable<Entry> createEntries(int count) {
List<Entry> entries = new ArrayList<>();
for (int i = 0; i < count; i++) {
entries.add(new Entry(22, i, i, "".getBytes()));
}
return Observable.fromIterable(entries);
}
示例7: getDirectoryFiles
import io.reactivex.Observable; //導入方法依賴的package包/類
public Observable<PFile> getDirectoryFiles(PFile directory) {
List<PFile> PFiles = getFilesFromCache(directory);
if (PFiles == null) {
return listFiles(directory);
}
return Observable.fromIterable(PFiles);
}
示例8: toObservableFromIterable
import io.reactivex.Observable; //導入方法依賴的package包/類
private Function<Collection<T>, Observable<T>> toObservableFromIterable() {
return new Function<Collection<T>, Observable<T>>() {
@Override
public Observable<T> apply(Collection<T> source) {
return Observable.fromIterable(source);
}
};
}
示例9: main
import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {
List<Beer> beers = loadCellar(); // populate the beer collection
System.out.println("== Observable creation from an Iterable");
Observable<Beer> observableBeer = Observable.fromIterable(beers);
observableBeer.subscribe(
beer -> System.out.println(beer),
error -> System.err.println(error),
() -> System.out.println("Streaming is over")
);
}
示例10: readMyDeviceLocalHistory
import io.reactivex.Observable; //導入方法依賴的package包/類
public Observable<IdentityProviderUsage> readMyDeviceLocalHistory(RoutingContext routingContext) {
LOG.debug("Received create IdentityProvider request");
String globalId = RequestReader.getCookieValue(routingContext, RequestReader.DEVICE_ID);
Device device = deviceFacade.read(new DeviceQuery().setGlobalId(globalId)).blockingGet();
return Observable.fromIterable(identityProviderUsageFacade.buildRecentHistory(device));
}
示例11: main
import io.reactivex.Observable; //導入方法依賴的package包/類
public static void main(String[] args) {
List<Beer> beers = loadCellar(); // populate the beer collection
// === Java 8 Stream
System.out.println("\n== Iterating over Java 8 Stream");
beers.stream()
.skip(1)
.limit(3)
.filter(b -> "USA".equals(b.country))
.map(b -> b.name + ": $" + b.price)
.forEach(beer -> System.out.println(beer));
// === RxJava Observable
Observable<Beer> observableBeer = null;
System.out.println("\n== Subscribing to Observable ");
observableBeer = Observable.fromIterable(beers);
observableBeer
.skip(1)
.take(3)
.filter(b -> "USA".equals(b.country))
.map(b -> b.name + ": $" + b.price)
.subscribe(
beer -> System.out.println(beer),
err -> System.out.println(err),
() -> System.out.println("Streaming is complete"),
disposable -> System.out.println( " !!! Someone just subscribed to the beer stream!!! ")
);
}
示例12: countStreamForTickFetch
import io.reactivex.Observable; //導入方法依賴的package包/類
private Observable<Long> countStreamForTickFetch(final long endTime) {
final LongStream counter = LongStream
.iterate(1, i -> i + 1)
.map(count -> endTime - count * tickFetchMillis + 1);
return Observable.fromIterable(counter::iterator);
}
示例13: filter
import io.reactivex.Observable; //導入方法依賴的package包/類
@Override
public Observable<DeviceAccess> filter(DeviceAccessQuery query) {
return Observable.fromIterable(deviceAccessList);
}
示例14: getDeptsRx
import io.reactivex.Observable; //導入方法依賴的package包/類
@Override
public Observable<Department> getDeptsRx() {
Observable<Department> depts= Observable.fromIterable(departmentDaoImpl.getDepartments());
return depts;
}
示例15: getEmployeesRx
import io.reactivex.Observable; //導入方法依賴的package包/類
@Override
public Observable<Employee> getEmployeesRx() {
Observable<Employee> publishedEmployees= Observable.fromIterable(employeeDaoImpl.getEmployees());
return publishedEmployees;
}