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

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

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

Flowable.toMap介绍

[英]Returns a Single that emits a single HashMap containing all items emitted by the finite source Publisher, mapped by the keys returned by a specified keySelector function.

If more than one source item maps to the same key, the HashMap will contain the latest of those items.

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: The operator honors backpressure from downstream and consumes the source Publisher in an unbounded manner (i.e., without applying backpressure to it). Scheduler: toMap does not operate by default on a particular Scheduler.
[中]返回一个函数,该函数发出一个HashMap,该HashMap包含有限源发布服务器发出的所有项,并由指定的keySelector函数返回的键进行映射。
如果多个源项映射到同一个键,HashMap将包含这些项中的最新项。
请注意,此运算符要求上游发出信号onComplete,以发出累积贴图。无限且永远不完整的源永远不会通过此运算符发出任何信息,无限源可能导致致命的OutOfMemoryError。背压:操作员接受来自下游的背压,并以无限制的方式消耗源发布服务器(即,不向其施加背压)。调度程序:默认情况下,toMap不会在特定调度程序上运行。

代码示例

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

@Test(expected = NullPointerException.class)
public void toMapValueNull() {
  just1.toMap(new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return v;
    }
  }, null);
}

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

@Test(expected = NullPointerException.class)
public void toMapKeyNullAllowed() {
  just1.toMap(null);
}

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

@Test(expected = NullPointerException.class)
public void toMapMapSupplierNull() {
  just1.toMap(new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return v;
    }
  }, new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return v;
    }
  }, null);
}

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

@Test
public void testToMap() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Single<Map<Integer, String>> mapped = source.toMap(lengthFunc);
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "a");
  expected.put(2, "bb");
  expected.put(3, "ccc");
  expected.put(4, "dddd");
  mapped.subscribe(singleObserver);
  verify(singleObserver, never()).onError(any(Throwable.class));
  verify(singleObserver, times(1)).onSuccess(expected);
}

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

@Test
public void testToMapWithValueSelector() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicate);
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "aa");
  expected.put(2, "bbbb");
  expected.put(3, "cccccc");
  expected.put(4, "dddddddd");
  mapped.subscribe(singleObserver);
  verify(singleObserver, never()).onError(any(Throwable.class));
  verify(singleObserver, times(1)).onSuccess(expected);
}

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

@Test
public void testToMapFlowable() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc).toFlowable();
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "a");
  expected.put(2, "bb");
  expected.put(3, "ccc");
  expected.put(4, "dddd");
  mapped.subscribe(objectSubscriber);
  verify(objectSubscriber, never()).onError(any(Throwable.class));
  verify(objectSubscriber, times(1)).onNext(expected);
  verify(objectSubscriber, times(1)).onComplete();
}

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

@Test
public void testToMapWithValueSelectorFlowable() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicate).toFlowable();
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "aa");
  expected.put(2, "bbbb");
  expected.put(3, "cccccc");
  expected.put(4, "dddddddd");
  mapped.subscribe(objectSubscriber);
  verify(objectSubscriber, never()).onError(any(Throwable.class));
  verify(objectSubscriber, times(1)).onNext(expected);
  verify(objectSubscriber, times(1)).onComplete();
}

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

@Test
public void testToMapWithErrorInValueSelector() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Function<String, String> duplicateErr = new Function<String, String>() {
    @Override
    public String apply(String t1) {
      if ("bb".equals(t1)) {
        throw new RuntimeException("Forced failure");
      }
      return t1 + t1;
    }
  };
  Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicateErr);
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "aa");
  expected.put(2, "bbbb");
  expected.put(3, "cccccc");
  expected.put(4, "dddddddd");
  mapped.subscribe(singleObserver);
  verify(singleObserver, never()).onSuccess(expected);
  verify(singleObserver, times(1)).onError(any(Throwable.class));
}

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

@Test
public void testToMapWithError() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
    @Override
    public Integer apply(String t1) {
      if ("bb".equals(t1)) {
        throw new RuntimeException("Forced Failure");
      }
      return t1.length();
    }
  };
  Single<Map<Integer, String>> mapped = source.toMap(lengthFuncErr);
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "a");
  expected.put(2, "bb");
  expected.put(3, "ccc");
  expected.put(4, "dddd");
  mapped.subscribe(singleObserver);
  verify(singleObserver, never()).onSuccess(expected);
  verify(singleObserver, times(1)).onError(any(Throwable.class));
}

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

@Test
public void toMapValueSelectorReturnsNull() {
  just1.toMap(new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return v;
    }
  }, new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return null;
    }
  }).blockingGet();
}

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

@Test
public void toMultiMapValueSelectorReturnsNullAllowed() {
  just1.toMap(new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return v;
    }
  }, new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return null;
    }
  }).blockingGet();
}

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

@Test
public void testToMapWithErrorThrowingFactory() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Callable<Map<Integer, String>> mapFactory = new Callable<Map<Integer, String>>() {
    @Override
    public Map<Integer, String> call() {
      throw new RuntimeException("Forced failure");
    }
  };
  Function<String, Integer> lengthFunc = new Function<String, Integer>() {
    @Override
    public Integer apply(String t1) {
      return t1.length();
    }
  };
  Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() {
    @Override
    public String apply(String v) {
      return v;
    }
  }, mapFactory);
  Map<Integer, String> expected = new LinkedHashMap<Integer, String>();
  expected.put(2, "bb");
  expected.put(3, "ccc");
  expected.put(4, "dddd");
  mapped.subscribe(singleObserver);
  verify(singleObserver, never()).onSuccess(expected);
  verify(singleObserver, times(1)).onError(any(Throwable.class));
}

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

@Test(expected = NullPointerException.class)
public void toMapMapSupplierReturnsNull() {
  just1.toMap(new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return v;
    }
  }, new Function<Integer, Object>() {
    @Override
    public Object apply(Integer v) {
      return v;
    }
  }, new Callable<Map<Object, Object>>() {
    @Override
    public Map<Object, Object> call() {
      return null;
    }
  }).blockingGet();
}

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

@Test
public void testToMapWithErrorFlowable() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
    @Override
    public Integer apply(String t1) {
      if ("bb".equals(t1)) {
        throw new RuntimeException("Forced Failure");
      }
      return t1.length();
    }
  };
  Flowable<Map<Integer, String>> mapped = source.toMap(lengthFuncErr).toFlowable();
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "a");
  expected.put(2, "bb");
  expected.put(3, "ccc");
  expected.put(4, "dddd");
  mapped.subscribe(objectSubscriber);
  verify(objectSubscriber, never()).onNext(expected);
  verify(objectSubscriber, never()).onComplete();
  verify(objectSubscriber, times(1)).onError(any(Throwable.class));
}

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

@Test
public void testToMapWithErrorInValueSelectorFlowable() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Function<String, String> duplicateErr = new Function<String, String>() {
    @Override
    public String apply(String t1) {
      if ("bb".equals(t1)) {
        throw new RuntimeException("Forced failure");
      }
      return t1 + t1;
    }
  };
  Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, duplicateErr).toFlowable();
  Map<Integer, String> expected = new HashMap<Integer, String>();
  expected.put(1, "aa");
  expected.put(2, "bbbb");
  expected.put(3, "cccccc");
  expected.put(4, "dddddddd");
  mapped.subscribe(objectSubscriber);
  verify(objectSubscriber, never()).onNext(expected);
  verify(objectSubscriber, never()).onComplete();
  verify(objectSubscriber, times(1)).onError(any(Throwable.class));
}

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

Single<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() {
  @Override
  public String apply(String v) {

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

@Override
public Publisher<Map<Integer, Integer>> createPublisher(final long elements) {
  return
      Flowable.range(1, 1000).toMap(Functions.<Integer>identity()).toFlowable()
    ;
}

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

@Test
public void testToMapWithErrorThrowingFactoryFlowable() {
  Flowable<String> source = Flowable.just("a", "bb", "ccc", "dddd");
  Callable<Map<Integer, String>> mapFactory = new Callable<Map<Integer, String>>() {
    @Override
    public Map<Integer, String> call() {
      throw new RuntimeException("Forced failure");
    }
  };
  Function<String, Integer> lengthFunc = new Function<String, Integer>() {
    @Override
    public Integer apply(String t1) {
      return t1.length();
    }
  };
  Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() {
    @Override
    public String apply(String v) {
      return v;
    }
  }, mapFactory).toFlowable();
  Map<Integer, String> expected = new LinkedHashMap<Integer, String>();
  expected.put(2, "bb");
  expected.put(3, "ccc");
  expected.put(4, "dddd");
  mapped.subscribe(objectSubscriber);
  verify(objectSubscriber, never()).onNext(expected);
  verify(objectSubscriber, never()).onComplete();
  verify(objectSubscriber, times(1)).onError(any(Throwable.class));
}

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

Flowable<Map<Integer, String>> mapped = source.toMap(lengthFunc, new Function<String, String>() {
  @Override
  public String apply(String v) {

代码示例来源:origin: SmartDataAnalytics/jena-sparql-api

default Map<K, V> fetchMap(Range<Long> range) {
    Map<K, V> result = apply(range)
        .toMap(Entry::getKey, Entry::getValue)
        .blockingGet();
//                .collect(Collectors.toMap(
//                        Entry::getKey,
//                        Entry::getValue,
//                        (u, v) -> { throw new IllegalStateException(String.format("Duplicate key %s", u)); },
//                        LinkedHashMap::new));
    return result;
  }
}

相关文章

Flowable类方法