本文整理了Java中io.reactivex.Observable.toMultimap()
方法的一些代码示例,展示了Observable.toMultimap()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.toMultimap()
方法的具体详情如下:
包路径:io.reactivex.Observable
类名称: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
@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
public void testToMultimapWithValueSelector() {
Observable<String> source = Observable.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() {
Observable<String> source = Observable.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(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 testToMultimapObservable() {
Observable<String> source = Observable.just("a", "b", "cc", "dd");
Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc).toObservable();
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(objectObserver);
verify(objectObserver, never()).onError(any(Throwable.class));
verify(objectObserver, times(1)).onNext(expected);
verify(objectObserver, times(1)).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithValueSelectorObservable() {
Observable<String> source = Observable.just("a", "b", "cc", "dd");
Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicate).toObservable();
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(objectObserver);
verify(objectObserver, never()).onError(any(Throwable.class));
verify(objectObserver, times(1)).onNext(expected);
verify(objectObserver, times(1)).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithError() {
Observable<String> source = Observable.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() {
Observable<String> source = Observable.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() {
Observable<String> source = Observable.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
Callable<Map<K, Collection<V>>> mapSupplier
) {
return toMultimap(keySelector, valueSelector, mapSupplier, ArrayListSupplier.<V, K>asFunction());
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithErrorObservable() {
Observable<String> source = Observable.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();
}
};
Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFuncErr).toObservable();
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(objectObserver);
verify(objectObserver, times(1)).onError(any(Throwable.class));
verify(objectObserver, never()).onNext(expected);
verify(objectObserver, never()).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithErrorInValueSelectorObservable() {
Observable<String> source = Observable.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;
}
};
Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(lengthFunc, duplicateErr).toObservable();
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(objectObserver);
verify(objectObserver, times(1)).onError(any(Throwable.class));
verify(objectObserver, never()).onNext(expected);
verify(objectObserver, never()).onComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testToMultimapWithMapThrowingFactoryObservable() {
Observable<String> source = Observable.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");
}
};
Observable<Map<Integer, Collection<String>>> mapped = source
.toMultimap(lengthFunc, new Function<String, String>() {
@Override
public String apply(String v) {
return v;
}
}, mapFactory).toObservable();
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(objectObserver);
verify(objectObserver, times(1)).onError(any(Throwable.class));
verify(objectObserver, never()).onNext(expected);
verify(objectObserver, 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
Observable<Map<Integer, Collection<String>>> mapped = source.toMultimap(
lengthFunc, identity,
mapFactory, new Function<Integer, Collection<String>>() {
代码示例来源: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
@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
/**
* Returns a Single that emits a single HashMap that contains an ArrayList of items emitted by the
* finite source ObservableSource keyed by a specified {@code keySelector} function.
* <p>
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/toMultiMap.2.png" alt="">
* <p>
* Note that this operator requires the upstream to signal {@code 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 {@code OutOfMemoryError}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code toMultimap} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <K> the key type of the Map
* @param keySelector
* the function that extracts the key from the source items to be used as key in the HashMap
* @return a Single that emits a single item: a HashMap that contains an ArrayList of items mapped from
* the source ObservableSource
* @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX operators documentation: To</a>
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <K> Single<Map<K, Collection<T>>> toMultimap(Function<? super T, ? extends K> keySelector) {
@SuppressWarnings({ "rawtypes", "unchecked" })
Function<? super T, ? extends T> valueSelector = (Function)Functions.identity();
Callable<Map<K, Collection<T>>> mapSupplier = HashMapSupplier.asCallable();
Function<K, List<T>> collectionFactory = ArrayListSupplier.asFunction();
return toMultimap(keySelector, valueSelector, mapSupplier, collectionFactory);
}
内容来源于网络,如有侵权,请联系作者删除!