io.reactivex.Observable.toMultimap()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(14.1k)|赞(0)|评价(0)|浏览(122)

本文整理了Java中io.reactivex.Observable.toMultimap()方法的一些代码示例,展示了Observable.toMultimap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.toMultimap()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:toMultimap

Observable.toMultimap介绍

[英]Returns a Single that emits a single HashMap that contains an ArrayList of items emitted by the finite source ObservableSource keyed by a specified keySelector function.

Note that this operator requires the upstream to signal onComplete for the accumulated map to be emitted. Sources that are infinite and never complete will never emit anything through this operator and an infinite source may lead to a fatal OutOfMemoryError. Scheduler: toMultimap does not operate by default on a particular Scheduler.
[中]返回一个函数,该函数发出一个HashMap,该HashMap包含由指定keySelector函数键控的有限源ObservableSource发出的项的ArrayList。
请注意,该运算符要求上游发出信号“onComplete”,以发出累积的map。无限且永远不完整的源永远不会通过该运算符发出任何信息,而无限源可能会导致致命的OutOfMemory错误。调度器:默认情况下,toMultimap不会在特定的调度器上运行。

代码示例

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void toMultimapValueNull() {
  3. just1.toMultimap(new Function<Integer, Object>() {
  4. @Override
  5. public Object apply(Integer v) {
  6. return v;
  7. }
  8. }, null);
  9. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void toMultimapKeyNull() {
  3. just1.toMultimap(null);
  4. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void toMultimapMapMapSupplierNull() {
  3. just1.toMultimap(new Function<Integer, Object>() {
  4. @Override
  5. public Object apply(Integer v) {
  6. return v;
  7. }
  8. }, new Function<Integer, Object>() {
  9. @Override
  10. public Object apply(Integer v) {
  11. return v;
  12. }
  13. }, null);
  14. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithValueSelector() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate);
  5. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  6. expected.put(1, Arrays.asList("aa", "bb"));
  7. expected.put(2, Arrays.asList("cccc", "dddd"));
  8. mapped.subscribe(singleObserver);
  9. verify(singleObserver, never()).onError(any(Throwable.class));
  10. verify(singleObserver, times(1)).onSuccess(expected);
  11. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimap() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc);
  5. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  6. expected.put(1, Arrays.asList("a", "b"));
  7. expected.put(2, Arrays.asList("cc", "dd"));
  8. mapped.subscribe(singleObserver);
  9. verify(singleObserver, never()).onError(any(Throwable.class));
  10. verify(singleObserver, times(1)).onSuccess(expected);
  11. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void toMultimapMapMapCollectionSupplierNull() {
  3. just1.toMultimap(new Function<Integer, Integer>() {
  4. @Override
  5. public Integer apply(Integer v) {
  6. return v;
  7. }
  8. }, new Function<Integer, Integer>() {
  9. @Override
  10. public Integer apply(Integer v) {
  11. return v;
  12. }
  13. }, new Callable<Map<Integer, Collection<Integer>>>() {
  14. @Override
  15. public Map<Integer, Collection<Integer>> call() {
  16. return new HashMap<Integer, Collection<Integer>>();
  17. }
  18. }, null);
  19. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapObservable() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc).toObservable();
  5. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  6. expected.put(1, Arrays.asList("a", "b"));
  7. expected.put(2, Arrays.asList("cc", "dd"));
  8. mapped.subscribe(objectObserver);
  9. verify(objectObserver, never()).onError(any(Throwable.class));
  10. verify(objectObserver, times(1)).onNext(expected);
  11. verify(objectObserver, times(1)).onComplete();
  12. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithValueSelectorObservable() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate).toObservable();
  5. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  6. expected.put(1, Arrays.asList("aa", "bb"));
  7. expected.put(2, Arrays.asList("cccc", "dddd"));
  8. mapped.subscribe(objectObserver);
  9. verify(objectObserver, never()).onError(any(Throwable.class));
  10. verify(objectObserver, times(1)).onNext(expected);
  11. verify(objectObserver, times(1)).onComplete();
  12. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithError() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
  5. @Override
  6. public Integer apply(String t1) {
  7. if ("b".equals(t1)) {
  8. throw new RuntimeException("Forced Failure");
  9. }
  10. return t1.length();
  11. }
  12. };
  13. Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr);
  14. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  15. expected.put(1, Arrays.asList("a", "b"));
  16. expected.put(2, Arrays.asList("cc", "dd"));
  17. mapped.subscribe(singleObserver);
  18. verify(singleObserver, times(1)).onError(any(Throwable.class));
  19. verify(singleObserver, never()).onSuccess(expected);
  20. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithErrorInValueSelector() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Function<String, String> duplicateErr = new Function<String, String>() {
  5. @Override
  6. public String apply(String t1) {
  7. if ("b".equals(t1)) {
  8. throw new RuntimeException("Forced failure");
  9. }
  10. return t1 + t1;
  11. }
  12. };
  13. Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr);
  14. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  15. expected.put(1, Arrays.asList("aa", "bb"));
  16. expected.put(2, Arrays.asList("cccc", "dddd"));
  17. mapped.subscribe(singleObserver);
  18. verify(singleObserver, times(1)).onError(any(Throwable.class));
  19. verify(singleObserver, never()).onSuccess(expected);
  20. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithMapThrowingFactory() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd", "eee", "fff");
  4. Callable<Map<Integer, Collection<String>>> mapFactory = new Callable<Map<Integer, Collection<String>>>() {
  5. @Override
  6. public Map<Integer, Collection<String>> call() {
  7. throw new RuntimeException("Forced failure");
  8. }
  9. };
  10. Single<Map<Integer, Collection<String>>> mapped = source
  11. .toMultimap(lengthFunc, new Function<String, String>() {
  12. @Override
  13. public String apply(String v) {
  14. return v;
  15. }
  16. }, mapFactory);
  17. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  18. expected.put(2, Arrays.asList("cc", "dd"));
  19. expected.put(3, Arrays.asList("eee", "fff"));
  20. mapped.subscribe(singleObserver);
  21. verify(singleObserver, times(1)).onError(any(Throwable.class));
  22. verify(singleObserver, never()).onSuccess(expected);
  23. }

代码示例来源:origin: ReactiveX/RxJava

  1. Callable<Map<K, Collection<V>>> mapSupplier
  2. ) {
  3. return toMultimap(keySelector, valueSelector, mapSupplier, ArrayListSupplier.<V, K>asFunction());

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithErrorObservable() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
  5. @Override
  6. public Integer apply(String t1) {
  7. if ("b".equals(t1)) {
  8. throw new RuntimeException("Forced Failure");
  9. }
  10. return t1.length();
  11. }
  12. };
  13. Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr).toObservable();
  14. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  15. expected.put(1, Arrays.asList("a", "b"));
  16. expected.put(2, Arrays.asList("cc", "dd"));
  17. mapped.subscribe(objectObserver);
  18. verify(objectObserver, times(1)).onError(any(Throwable.class));
  19. verify(objectObserver, never()).onNext(expected);
  20. verify(objectObserver, never()).onComplete();
  21. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithErrorInValueSelectorObservable() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd");
  4. Function<String, String> duplicateErr = new Function<String, String>() {
  5. @Override
  6. public String apply(String t1) {
  7. if ("b".equals(t1)) {
  8. throw new RuntimeException("Forced failure");
  9. }
  10. return t1 + t1;
  11. }
  12. };
  13. Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr).toObservable();
  14. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  15. expected.put(1, Arrays.asList("aa", "bb"));
  16. expected.put(2, Arrays.asList("cccc", "dddd"));
  17. mapped.subscribe(objectObserver);
  18. verify(objectObserver, times(1)).onError(any(Throwable.class));
  19. verify(objectObserver, never()).onNext(expected);
  20. verify(objectObserver, never()).onComplete();
  21. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testToMultimapWithMapThrowingFactoryObservable() {
  3. Observable<String> source = Observable.just("a", "b", "cc", "dd", "eee", "fff");
  4. Callable<Map<Integer, Collection<String>>> mapFactory = new Callable<Map<Integer, Collection<String>>>() {
  5. @Override
  6. public Map<Integer, Collection<String>> call() {
  7. throw new RuntimeException("Forced failure");
  8. }
  9. };
  10. Observable<Map<Integer, Collection<String>>> mapped = source
  11. .toMultimap(lengthFunc, new Function<String, String>() {
  12. @Override
  13. public String apply(String v) {
  14. return v;
  15. }
  16. }, mapFactory).toObservable();
  17. Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
  18. expected.put(2, Arrays.asList("cc", "dd"));
  19. expected.put(3, Arrays.asList("eee", "fff"));
  20. mapped.subscribe(objectObserver);
  21. verify(objectObserver, times(1)).onError(any(Throwable.class));
  22. verify(objectObserver, never()).onNext(expected);
  23. verify(objectObserver, never()).onComplete();
  24. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void toMultimapMapSupplierReturnsNull() {
  3. just1.toMultimap(new Function<Integer, Object>() {
  4. @Override
  5. public Object apply(Integer v) {
  6. return v;
  7. }
  8. }, new Function<Integer, Object>() {
  9. @Override
  10. public Object apply(Integer v) {
  11. return v;
  12. }
  13. }, new Callable<Map<Object, Collection<Object>>>() {
  14. @Override
  15. public Map<Object, Collection<Object>> call() {
  16. return null;
  17. }
  18. }).blockingGet();
  19. }

代码示例来源:origin: ReactiveX/RxJava

  1. Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(
  2. lengthFunc, identity,
  3. mapFactory, new Function<Integer, Collection<String>>() {

代码示例来源:origin: ReactiveX/RxJava

  1. Callable<Map<K, Collection<V>>> mapSupplier = HashMapSupplier.asCallable();
  2. Function<K, List<V>> collectionFactory = ArrayListSupplier.asFunction();
  3. return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory);

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NullPointerException.class)
  2. public void toMultimapMapCollectionSupplierReturnsNull() {
  3. just1.toMultimap(new Function<Integer, Integer>() {
  4. @Override
  5. public Integer apply(Integer v) {
  6. return v;
  7. }
  8. }, new Function<Integer, Integer>() {
  9. @Override
  10. public Integer apply(Integer v) {
  11. return v;
  12. }
  13. }, new Callable<Map<Integer, Collection<Integer>>>() {
  14. @Override
  15. public Map<Integer, Collection<Integer>> call() {
  16. return new HashMap<Integer, Collection<Integer>>();
  17. }
  18. }, new Function<Integer, Collection<Integer>>() {
  19. @Override
  20. public Collection<Integer> apply(Integer v) {
  21. return null;
  22. }
  23. }).blockingGet();
  24. }

代码示例来源:origin: ReactiveX/RxJava

  1. /**
  2. * Returns a Single that emits a single HashMap that contains an ArrayList of items emitted by the
  3. * finite source ObservableSource keyed by a specified {@code keySelector} function.
  4. * <p>
  5. * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/toMultiMap.2.png" alt="">
  6. * <p>
  7. * Note that this operator requires the upstream to signal {@code onComplete} for the accumulated map to
  8. * be emitted. Sources that are infinite and never complete will never emit anything through this
  9. * operator and an infinite source may lead to a fatal {@code OutOfMemoryError}.
  10. * <dl>
  11. * <dt><b>Scheduler:</b></dt>
  12. * <dd>{@code toMultimap} does not operate by default on a particular {@link Scheduler}.</dd>
  13. * </dl>
  14. *
  15. * @param <K> the key type of the Map
  16. * @param keySelector
  17. * the function that extracts the key from the source items to be used as key in the HashMap
  18. * @return a Single that emits a single item: a HashMap that contains an ArrayList of items mapped from
  19. * the source ObservableSource
  20. * @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX operators documentation: To</a>
  21. */
  22. @CheckReturnValue
  23. @SchedulerSupport(SchedulerSupport.NONE)
  24. public final <K> Single<Map<K, Collection<T>>> toMultimap(Function<? super T, ? extends K> keySelector) {
  25. @SuppressWarnings({ "rawtypes", "unchecked" })
  26. Function<? super T, ? extends T> valueSelector = (Function)Functions.identity();
  27. Callable<Map<K, Collection<T>>> mapSupplier = HashMapSupplier.asCallable();
  28. Function<K, List<T>> collectionFactory = ArrayListSupplier.asFunction();
  29. return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory);
  30. }

相关文章

Observable类方法