Java Code Examples for java.util.stream.Collectors#toCollection()

The following examples show how to use java.util.stream.Collectors#toCollection() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@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);
}
 
Example 2
@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);
}
 
Example 3
@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);
}
 
Example 4
Source Project: vividus   File: ExcelSheetParser.java    License: Apache License 2.0 4 votes vote down vote up
private static <T> Collector<T, ?, LinkedList<T>> toLinkedList()
{
    return Collectors.toCollection(LinkedList::new);
}
 
Example 5
Source Project: hugegraph   File: HugeTask.java    License: Apache License 2.0 4 votes vote down vote up
private static <V> Collector<V, ?, Set<V>> toOrderSet() {
    return Collectors.toCollection(InsertionOrderUtil::newSet);
}
 
Example 6
Source Project: scipio-erp   File: UtilStream.java    License: Apache License 2.0 4 votes vote down vote up
public static <T> Collector<T, ?, List<T>> toArrayList(int initialCapacity) {
    return Collectors.toCollection(() -> new ArrayList<T>());
}
 
Example 7
Source Project: cyclops   File: SetX.java    License: Apache License 2.0 4 votes vote down vote up
static <T> Collector<T, ?, SetX<T>> setXCollector() {
    return Collectors.toCollection(() -> SetX.of());
}
 
Example 8
Source Project: cyclops   File: SetX.java    License: Apache License 2.0 4 votes vote down vote up
static <T> Collector<T, ?, Set<T>> defaultCollector() {
    return Collectors.toCollection(() -> new HashSet<>());
}
 
Example 9
Source Project: cyclops   File: SortedSetX.java    License: Apache License 2.0 4 votes vote down vote up
static <T> Collector<T, ?, SortedSet<T>> defaultCollector() {
    return Collectors.toCollection(() -> new TreeSet<T>(
                                                        (Comparator) Comparator.<Comparable> naturalOrder()));
}
 
Example 10
Source Project: cyclops   File: ListX.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return A JDK 8 Collector for converting Streams into ListX instances
 */
static <T> Collector<T, ?, ListX<T>> listXCollector() {
    return Collectors.toCollection(() -> ListX.of());
}
 
Example 11
Source Project: cyclops   File: ListX.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return An Array List Collector
 */
static <T> Collector<T, ?, List<T>> defaultCollector() {
    return Collectors.toCollection(() -> new ArrayList<>());
}
 
Example 12
Source Project: semagrow   File: PlanCollectionImpl.java    License: Apache License 2.0 4 votes vote down vote up
public static Collector<Plan, ?, PlanCollection> toPlanCollection(TupleExpr logical) {
    return Collectors.toCollection(() -> new PlanCollectionImpl(logical));
}
 
Example 13
Source Project: semagrow   File: PlanCollectionImpl.java    License: Apache License 2.0 4 votes vote down vote up
public static Collector<Plan, ?, PlanCollection> toPlanCollection(Set<TupleExpr> logical) {
    return Collectors.toCollection(() -> new PlanCollectionImpl(logical));
}
 
Example 14
/**
 * 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));
}
 
Example 15
Source Project: buck   File: TreeBackedProcessorWrapper.java    License: Apache License 2.0 4 votes vote down vote up
private Collector<TypeElement, ?, Set<TypeElement>> toSet() {
  return Collectors.toCollection(LinkedHashSet::new);
}
 
Example 16
Source Project: buck   File: TreeBackedRoundEnvironment.java    License: Apache License 2.0 4 votes vote down vote up
private Collector<Element, ?, Set<Element>> toSet() {
  return Collectors.toCollection(LinkedHashSet::new);
}