本文整理了Java中io.reactivex.Flowable.toMultimap()
方法的一些代码示例,展示了Flowable.toMultimap()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Flowable.toMultimap()
方法的具体详情如下:
包路径:io.reactivex.Flowable
类名称: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
@Test(expected = NullPointerException.class)
public void toMultimapValueNull() {
just1.toMultimap(new Function<Integer, Object>() {
@Override
public Object apply(Integer v) {
return v;
}
}, null);
}
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void toMultimapKeyNull() {
just1.toMultimap(null);
}
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void toMultimapMapMapSupplierNull() {
just1.toMultimap(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(expected = NullPointerException.class)
public void toMultimapMapMapCollectionSupplierNull() {
just1.toMultimap(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer v) {
return v;
}
}, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer v) {
return v;
}
}, new Callable<Map<Integer, Collection<Integer>>>() {
@Override
public Map<Integer, Collection<Integer>> call() {
return new HashMap<Integer, Collection<Integer>>();
}
}, null);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithValueSelector() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate);
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("aa", "bb"));
expected.put(2, Arrays.asList("cccc", "dddd"));
mapped.subscribe(singleObserver);
verify(singleObserver, never()).onError(any(Throwable.class));
verify(singleObserver, times(1)).onSuccess(expected);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimap() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc);
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("a", "b"));
expected.put(2, Arrays.asList("cc", "dd"));
mapped.subscribe(singleObserver);
verify(singleObserver, never()).onError(any(Throwable.class));
verify(singleObserver, times(1)).onSuccess(expected);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapFlowable() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc).toFlowable();
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("a", "b"));
expected.put(2, Arrays.asList("cc", "dd"));
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 testToMultimapWithValueSelectorFlowable() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate).toFlowable();
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("aa", "bb"));
expected.put(2, Arrays.asList("cccc", "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 testToMultimapWithError() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
@Override
public Integer apply(String t1) {
if ("b".equals(t1)) {
throw new RuntimeException("Forced Failure");
}
return t1.length();
}
};
Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr);
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("a", "b"));
expected.put(2, Arrays.asList("cc", "dd"));
mapped.subscribe(singleObserver);
verify(singleObserver, times(1)).onError(any(Throwable.class));
verify(singleObserver, never()).onSuccess(expected);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithErrorInValueSelector() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Function<String, String> duplicateErr = new Function<String, String>() {
@Override
public String apply(String t1) {
if ("b".equals(t1)) {
throw new RuntimeException("Forced failure");
}
return t1 + t1;
}
};
Single<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr);
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("aa", "bb"));
expected.put(2, Arrays.asList("cccc", "dddd"));
mapped.subscribe(singleObserver);
verify(singleObserver, times(1)).onError(any(Throwable.class));
verify(singleObserver, never()).onSuccess(expected);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithMapThrowingFactory() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd", "eee", "fff");
Callable<Map<Integer, Collection<String>>> mapFactory = new Callable<Map<Integer, Collection<String>>>() {
@Override
public Map<Integer, Collection<String>> call() {
throw new RuntimeException("Forced failure");
}
};
Single<Map<Integer, Collection<String>>> mapped = source
.toMultimap(lengthFunc, new Function<String, String>() {
@Override
public String apply(String v) {
return v;
}
}, mapFactory);
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(2, Arrays.asList("cc", "dd"));
expected.put(3, Arrays.asList("eee", "fff"));
mapped.subscribe(singleObserver);
verify(singleObserver, times(1)).onError(any(Throwable.class));
verify(singleObserver, never()).onSuccess(expected);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithErrorFlowable() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Function<String, Integer> lengthFuncErr = new Function<String, Integer>() {
@Override
public Integer apply(String t1) {
if ("b".equals(t1)) {
throw new RuntimeException("Forced Failure");
}
return t1.length();
}
};
Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr).toFlowable();
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("a", "b"));
expected.put(2, Arrays.asList("cc", "dd"));
mapped.subscribe(objectSubscriber);
verify(objectSubscriber, times(1)).onError(any(Throwable.class));
verify(objectSubscriber, never()).onNext(expected);
verify(objectSubscriber, never()).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithErrorInValueSelectorFlowable() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd");
Function<String, String> duplicateErr = new Function<String, String>() {
@Override
public String apply(String t1) {
if ("b".equals(t1)) {
throw new RuntimeException("Forced failure");
}
return t1 + t1;
}
};
Flowable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr).toFlowable();
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(1, Arrays.asList("aa", "bb"));
expected.put(2, Arrays.asList("cccc", "dddd"));
mapped.subscribe(objectSubscriber);
verify(objectSubscriber, times(1)).onError(any(Throwable.class));
verify(objectSubscriber, never()).onNext(expected);
verify(objectSubscriber, never()).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void toMultimapMapSupplierReturnsNull() {
just1.toMultimap(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, Collection<Object>>>() {
@Override
public Map<Object, Collection<Object>> call() {
return null;
}
}).blockingGet();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithMapThrowingFactoryFlowable() {
Flowable<String> source = Flowable.just("a", "b", "cc", "dd", "eee", "fff");
Callable<Map<Integer, Collection<String>>> mapFactory = new Callable<Map<Integer, Collection<String>>>() {
@Override
public Map<Integer, Collection<String>> call() {
throw new RuntimeException("Forced failure");
}
};
Flowable<Map<Integer, Collection<String>>> mapped = source
.toMultimap(lengthFunc, new Function<String, String>() {
@Override
public String apply(String v) {
return v;
}
}, mapFactory).toFlowable();
Map<Integer, Collection<String>> expected = new HashMap<Integer, Collection<String>>();
expected.put(2, Arrays.asList("cc", "dd"));
expected.put(3, Arrays.asList("eee", "fff"));
mapped.subscribe(objectSubscriber);
verify(objectSubscriber, times(1)).onError(any(Throwable.class));
verify(objectSubscriber, never()).onNext(expected);
verify(objectSubscriber, never()).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void toMultimapMapCollectionSupplierReturnsNull() {
just1.toMultimap(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer v) {
return v;
}
}, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer v) {
return v;
}
}, new Callable<Map<Integer, Collection<Integer>>>() {
@Override
public Map<Integer, Collection<Integer>> call() {
return new HashMap<Integer, Collection<Integer>>();
}
}, new Function<Integer, Collection<Integer>>() {
@Override
public Collection<Integer> apply(Integer v) {
return null;
}
}).blockingGet();
}
代码示例来源:origin: ReactiveX/RxJava
@Override
public Publisher<Map<Integer, Collection<Integer>>> createPublisher(final long elements) {
return
Flowable.range(1, 1000).toMultimap(Functions.<Integer>identity()).toFlowable()
;
}
代码示例来源:origin: ReactiveX/RxJava
.toMultimap(lengthFunc, identity, mapSupplier, collectionFactory).toFlowable();
代码示例来源:origin: ReactiveX/RxJava
Callable<Map<K, Collection<V>>> mapSupplier = HashMapSupplier.asCallable();
Function<K, List<V>> collectionFactory = ArrayListSupplier.asFunction();
return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory);
代码示例来源:origin: ReactiveX/RxJava
Callable<Map<K, Collection<T>>> mapSupplier = HashMapSupplier.asCallable();
Function<K, List<T>> collectionFactory = ArrayListSupplier.asFunction();
return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory);
内容来源于网络,如有侵权,请联系作者删除!