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

x33g5p2x  于2022-01-19 转载在 其他  
字(12.7k)|赞(0)|评价(0)|浏览(193)

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

Flowable.toMultimap介绍

[英]Returns a Single that emits a single HashMap that contains an ArrayList of items emitted by the finite source Publisher 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. Backpressure: This operator does not support backpressure as by intent it is requesting and buffering everything. Scheduler: toMultimap does not operate by default on a particular Scheduler.
[中]返回发出单个HashMap的单个HashMap,该HashMap包含由指定keySelector函数键入的有限源发布服务器发出的项的ArrayList。
请注意,此运算符要求上游发出信号onComplete,以发出累积贴图。无限且永远不完整的源永远不会通过此运算符发出任何信息,无限源可能导致致命的OutOfMemoryError。背压:该操作员不支持背压,因为它有意请求和缓冲所有内容。调度程序:默认情况下,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(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 testToMultimapWithValueSelector() {
  3. Flowable<String> source = Flowable.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. Flowable<String> source = Flowable.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
  2. public void testToMultimapFlowable() {
  3. Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
  4. Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc).toFlowable();
  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(objectSubscriber);
  9. verify(objectSubscriber, never()).onError(any(Throwable.class));
  10. verify(objectSubscriber, times(1)).onNext(expected);
  11. verify(objectSubscriber, times(1)).onComplete();
  12. }

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

  1. @Test
  2. public void testToMultimapWithValueSelectorFlowable() {
  3. Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
  4. Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate).toFlowable();
  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(objectSubscriber);
  9. verify(objectSubscriber, never()).onError(any(Throwable.class));
  10. verify(objectSubscriber, times(1)).onNext(expected);
  11. verify(objectSubscriber, times(1)).onComplete();
  12. }

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

  1. @Test
  2. public void testToMultimapWithError() {
  3. Flowable<String> source = Flowable.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. Flowable<String> source = Flowable.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. Flowable<String> source = Flowable.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. @Test
  2. public void testToMultimapWithErrorFlowable() {
  3. Flowable<String> source = Flowable.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. Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr).toFlowable();
  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(objectSubscriber);
  18. verify(objectSubscriber, times(1)).onError(any(Throwable.class));
  19. verify(objectSubscriber, never()).onNext(expected);
  20. verify(objectSubscriber, never()).onComplete();
  21. }

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

  1. @Test
  2. public void testToMultimapWithErrorInValueSelectorFlowable() {
  3. Flowable<String> source = Flowable.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. Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr).toFlowable();
  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(objectSubscriber);
  18. verify(objectSubscriber, times(1)).onError(any(Throwable.class));
  19. verify(objectSubscriber, never()).onNext(expected);
  20. verify(objectSubscriber, never()).onComplete();
  21. }

代码示例来源: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. @Test
  2. public void testToMultimapWithMapThrowingFactoryFlowable() {
  3. Flowable<String> source = Flowable.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. Flowable<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).toFlowable();
  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(objectSubscriber);
  21. verify(objectSubscriber, times(1)).onError(any(Throwable.class));
  22. verify(objectSubscriber, never()).onNext(expected);
  23. verify(objectSubscriber, never()).onComplete();
  24. }

代码示例来源: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. @Override
  2. public Publisher<Map<Integer, Collection<Integer>>> createPublisher(final long elements) {
  3. return
  4. Flowable.range(1, 1000).toMultimap(Functions.<Integer>identity()).toFlowable()
  5. ;
  6. }

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

  1. .toMultimap(lengthFunc, identity, mapSupplier, collectionFactory).toFlowable();

代码示例来源: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. Callable<Map<K, Collection<T>>> mapSupplier = HashMapSupplier.asCallable();
  2. Function<K, List<T>> collectionFactory = ArrayListSupplier.asFunction();
  3. return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory);

相关文章

Flowable类方法