本文整理了Java中io.reactivex.Observable.collect()
方法的一些代码示例,展示了Observable.collect()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.collect()
方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:collect
[英]Collects items emitted by the finite source ObservableSource into a single mutable data structure and returns a Single that emits this structure.
This is a simplified version of reduce that does not need to return the state on each pass.
Note that this operator requires the upstream to signal onComplete for the accumulator object 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: collect does not operate by default on a particular Scheduler.
[中]将有限源ObservableSource发出的项收集到单个可变数据结构中,并返回发出此结构的单个数据结构。
这是reduce的简化版本,不需要在每次传递时返回状态。
请注意,此运算符要求上游发出信号,通知要发射的累加器对象完成。无限且永远不完整的源永远不会通过该运算符发出任何信息,而无限源可能会导致致命的OutOfMemory错误。调度程序:默认情况下,collect不会在特定调度程序上运行。
代码示例来源:origin: ReactiveX/RxJava
@Override
public SingleSource<List<Integer>> apply(Observable<Integer> o) throws Exception {
return o.collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() throws Exception {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> a, Integer b) throws Exception {
a.add(b);
}
});
}
});
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void collectInitialCollectorNull() {
just1.collect(new Callable<Object>() {
@Override
public Object call() {
return 1;
}
}, null);
}
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void collectInitialSupplierNull() {
just1.collect((Callable<Integer>)null, new BiConsumer<Integer, Integer>() {
@Override
public void accept(Integer a, Integer b) { }
});
}
代码示例来源:origin: ReactiveX/RxJava
@Override
public ObservableSource<List<Integer>> apply(Observable<Integer> o) throws Exception {
return o.collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() throws Exception {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> a, Integer b) throws Exception {
a.add(b);
}
}).toObservable();
}
});
代码示例来源:origin: ReactiveX/RxJava
@Override
public Object apply(Observable<Integer> o) throws Exception {
return o.collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() throws Exception {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> a, Integer b) throws Exception {
a.add(b);
}
}).toObservable();
}
}, false, 1, 2, Arrays.asList(1));
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void collectInitialSupplierReturnsNull() {
just1.collect(new Callable<Object>() {
@Override
public Object call() {
return null;
}
}, new BiConsumer<Object, Integer>() {
@Override
public void accept(Object a, Integer b) { }
}).blockingGet();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectToString() {
String value = Observable.just(1, 2, 3).collect(new Callable<StringBuilder>() {
@Override
public StringBuilder call() {
return new StringBuilder();
}
},
new BiConsumer<StringBuilder, Integer>() {
@Override
public void accept(StringBuilder sb, Integer v) {
if (sb.length() > 0) {
sb.append("-");
}
sb.append(v);
}
}).blockingGet().toString();
assertEquals("1-2-3", value);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectToStringObservable() {
String value = Observable.just(1, 2, 3).collect(new Callable<StringBuilder>() {
@Override
public StringBuilder call() {
return new StringBuilder();
}
},
new BiConsumer<StringBuilder, Integer>() {
@Override
public void accept(StringBuilder sb, Integer v) {
if (sb.length() > 0) {
sb.append("-");
}
sb.append(v);
}
}).toObservable().blockingLast().toString();
assertEquals("1-2-3", value);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectToList() {
Single<List<Integer>> o = Observable.just(1, 2, 3)
.collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> list, Integer v) {
list.add(v);
}
});
List<Integer> list = o.blockingGet();
assertEquals(3, list.size());
assertEquals(1, list.get(0).intValue());
assertEquals(2, list.get(1).intValue());
assertEquals(3, list.get(2).intValue());
// test multiple subscribe
List<Integer> list2 = o.blockingGet();
assertEquals(3, list2.size());
assertEquals(1, list2.get(0).intValue());
assertEquals(2, list2.get(1).intValue());
assertEquals(3, list2.get(2).intValue());
}
代码示例来源:origin: ReactiveX/RxJava
/**
* Returns a Single that emits a single HashMap containing all items emitted by the
* finite source ObservableSource, mapped by the keys returned by a specified
* {@code keySelector} function.
* <p>
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/toMap.2.png" alt="">
* <p>
* If more than one source item maps to the same key, the HashMap will contain the latest of those items.
* <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 toMap} 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 a source item to be used in the HashMap
* @return a Single that emits a single item: a HashMap containing the mapped items 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, T>> toMap(final Function<? super T, ? extends K> keySelector) {
ObjectHelper.requireNonNull(keySelector, "keySelector is null");
return collect(HashMapSupplier.<K, T>asCallable(), Functions.toMapKeySelector(keySelector));
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectToListObservable() {
Observable<List<Integer>> o = Observable.just(1, 2, 3)
.collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> list, Integer v) {
list.add(v);
}
}).toObservable();
List<Integer> list = o.blockingLast();
assertEquals(3, list.size());
assertEquals(1, list.get(0).intValue());
assertEquals(2, list.get(1).intValue());
assertEquals(3, list.get(2).intValue());
// test multiple subscribe
List<Integer> list2 = o.blockingLast();
assertEquals(3, list2.size());
assertEquals(1, list2.get(0).intValue());
assertEquals(2, list2.get(1).intValue());
assertEquals(3, list2.get(2).intValue());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void dispose() {
TestHelper.checkDisposed(Observable.range(1, 3).collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() throws Exception {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> a, Integer b) throws Exception {
a.add(b);
}
}));
TestHelper.checkDisposed(Observable.range(1, 3).collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() throws Exception {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> a, Integer b) throws Exception {
a.add(b);
}
}).toObservable());
}
代码示例来源:origin: ReactiveX/RxJava
ObjectHelper.requireNonNull(mapSupplier, "mapSupplier is null");
ObjectHelper.requireNonNull(collectionFactory, "collectionFactory is null");
return collect(mapSupplier, Functions.toMultimapKeyValueSelector(keySelector, valueSelector, collectionFactory));
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectorFailureDoesNotResultInTwoErrorEmissions() {
try {
final List<Throwable> list = new CopyOnWriteArrayList<Throwable>();
RxJavaPlugins.setErrorHandler(addToList(list));
final RuntimeException e1 = new RuntimeException();
final RuntimeException e2 = new RuntimeException();
Burst.items(1).error(e2) //
.collect(callableListCreator(), biConsumerThrows(e1)) //
.test() //
.assertError(e1) //
.assertNotComplete();
assertEquals(1, list.size());
assertEquals(e2, list.get(0).getCause());
} finally {
RxJavaPlugins.reset();
}
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectorFailureDoesNotResultInTwoErrorEmissionsObservable() {
try {
final List<Throwable> list = new CopyOnWriteArrayList<Throwable>();
RxJavaPlugins.setErrorHandler(addToList(list));
final RuntimeException e1 = new RuntimeException();
final RuntimeException e2 = new RuntimeException();
Burst.items(1).error(e2) //
.collect(callableListCreator(), biConsumerThrows(e1)) //
.toObservable()
.test() //
.assertError(e1) //
.assertNotComplete();
assertEquals(1, list.size());
assertEquals(e2, list.get(0).getCause());
} finally {
RxJavaPlugins.reset();
}
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectorFailureDoesNotResultInErrorAndCompletedEmissions() {
final RuntimeException e = new RuntimeException();
Burst.item(1).create() //
.collect(callableListCreator(), biConsumerThrows(e)) //
.test() //
.assertError(e) //
.assertNotComplete();
}
代码示例来源:origin: ReactiveX/RxJava
/**
* This uses the public API collect which uses scan under the covers.
*/
@Test
public void testSeedFactory() {
Observable<List<Integer>> o = Observable.range(1, 10)
.collect(new Callable<List<Integer>>() {
@Override
public List<Integer> call() {
return new ArrayList<Integer>();
}
}, new BiConsumer<List<Integer>, Integer>() {
@Override
public void accept(List<Integer> list, Integer t2) {
list.add(t2);
}
}).toObservable().takeLast(1);
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.blockingSingle());
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.blockingSingle());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectorFailureDoesNotResultInErrorAndOnNextEmissionsObservable() {
final RuntimeException e = new RuntimeException();
final AtomicBoolean added = new AtomicBoolean();
BiConsumer<Object, Integer> throwOnFirstOnly = new BiConsumer<Object, Integer>() {
boolean once = true;
@Override
public void accept(Object o, Integer t) {
if (once) {
once = false;
throw e;
} else {
added.set(true);
}
}
};
Burst.items(1, 2).create() //
.collect(callableListCreator(), throwOnFirstOnly)//
.test() //
.assertError(e) //
.assertNoValues() //
.assertNotComplete();
assertFalse(added.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectorFailureDoesNotResultInErrorAndOnNextEmissions() {
final RuntimeException e = new RuntimeException();
final AtomicBoolean added = new AtomicBoolean();
BiConsumer<Object, Integer> throwOnFirstOnly = new BiConsumer<Object, Integer>() {
boolean once = true;
@Override
public void accept(Object o, Integer t) {
if (once) {
once = false;
throw e;
} else {
added.set(true);
}
}
};
Burst.items(1, 2).create() //
.collect(callableListCreator(), throwOnFirstOnly)//
.test() //
.assertError(e) //
.assertNoValues() //
.assertNotComplete();
assertFalse(added.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCollectorFailureDoesNotResultInErrorAndCompletedEmissionsObservable() {
final RuntimeException e = new RuntimeException();
Burst.item(1).create() //
.collect(callableListCreator(), biConsumerThrows(e)) //
.toObservable()
.test() //
.assertError(e) //
.assertNotComplete();
}
内容来源于网络,如有侵权,请联系作者删除!