下面列出了java.util.stream.Collectors#toCollection ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void discardIntermediateListElementsOnError() {
final Collector<Integer, ?, Collection<Integer>> collector = Collectors.toCollection(ArrayList::new);
Mono<Collection<Integer>> test =
Flux.range(1, 10)
.hide()
.map(i -> {
if (i == 5) {
throw new IllegalStateException("boom");
}
return i;
})
.collect(collector);
StepVerifier.create(test)
.expectErrorMessage("boom")
.verifyThenAssertThat()
.hasDiscardedExactly(1, 2, 3, 4);
}
@Override
public Object apply(Object value) {
if (!(value instanceof Iterable)) {
return value;
}
if (value instanceof HashSet || value instanceof ArrayList || value instanceof TreeSet) {
// don't convert java collections (assume no guava implementations exists inside it)
return value;
}
// transform to java collections
Collector<Object, ?, ? extends Iterable<Object>> collector;
if (value instanceof SortedSet) {
collector = Collectors.toCollection(TreeSet::new);
} else if (value instanceof Set) {
collector = Collectors.toCollection(HashSet::new);
} else {
collector = Collectors.toList();
}
return StreamSupport.stream(((Iterable<Object>) value).spliterator(), false).map(this::apply).collect(collector);
}
@Test
public void discardIntermediateListElementsOnCancel() {
final Collector<Long, ?, Collection<Long>> collector = Collectors.toCollection(ArrayList::new);
StepVerifier.withVirtualTime(() ->
Flux.interval(Duration.ofMillis(100))
.take(10)
.collect(collector)
)
.expectSubscription()
.expectNoEvent(Duration.ofMillis(210))
.thenCancel()
.verifyThenAssertThat()
.hasDiscardedExactly(0L, 1L);
}
private static <T> Collector<T, ?, LinkedList<T>> toLinkedList()
{
return Collectors.toCollection(LinkedList::new);
}
private static <V> Collector<V, ?, Set<V>> toOrderSet() {
return Collectors.toCollection(InsertionOrderUtil::newSet);
}
public static <T> Collector<T, ?, List<T>> toArrayList(int initialCapacity) {
return Collectors.toCollection(() -> new ArrayList<T>());
}
static <T> Collector<T, ?, SetX<T>> setXCollector() {
return Collectors.toCollection(() -> SetX.of());
}
static <T> Collector<T, ?, Set<T>> defaultCollector() {
return Collectors.toCollection(() -> new HashSet<>());
}
static <T> Collector<T, ?, SortedSet<T>> defaultCollector() {
return Collectors.toCollection(() -> new TreeSet<T>(
(Comparator) Comparator.<Comparable> naturalOrder()));
}
/**
* @return A JDK 8 Collector for converting Streams into ListX instances
*/
static <T> Collector<T, ?, ListX<T>> listXCollector() {
return Collectors.toCollection(() -> ListX.of());
}
/**
* @return An Array List Collector
*/
static <T> Collector<T, ?, List<T>> defaultCollector() {
return Collectors.toCollection(() -> new ArrayList<>());
}
public static Collector<Plan, ?, PlanCollection> toPlanCollection(TupleExpr logical) {
return Collectors.toCollection(() -> new PlanCollectionImpl(logical));
}
public static Collector<Plan, ?, PlanCollection> toPlanCollection(Set<TupleExpr> logical) {
return Collectors.toCollection(() -> new PlanCollectionImpl(logical));
}
/**
* Provides a stream collector that will collect items into an array list with a given initial capacity.
*/
public static <T> Collector<T, ?, ArrayList<T>> arrayListCollector(final int size) {
return Collectors.toCollection( () -> new ArrayList<>(size));
}
private Collector<TypeElement, ?, Set<TypeElement>> toSet() {
return Collectors.toCollection(LinkedHashSet::new);
}
private Collector<Element, ?, Set<Element>> toSet() {
return Collectors.toCollection(LinkedHashSet::new);
}