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

The following examples show how to use java.util.stream.Collectors#toCollection() . 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
Source File: MonoStreamCollectorTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@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
Source File: BindVariableConverter.java    From immutables with Apache License 2.0 6 votes vote down vote up
@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
Source File: MonoStreamCollectorTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@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 File: ExcelSheetParser.java    From vividus with 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 File: HugeTask.java    From hugegraph with 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 File: UtilStream.java    From scipio-erp with 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 File: SetX.java    From cyclops with 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 File: SetX.java    From cyclops with 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 File: SortedSetX.java    From cyclops with 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 File: ListX.java    From cyclops with 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 File: ListX.java    From cyclops with 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 File: PlanCollectionImpl.java    From semagrow with 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 File: PlanCollectionImpl.java    From semagrow with 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
Source File: SVUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * 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 File: TreeBackedProcessorWrapper.java    From buck with Apache License 2.0 4 votes vote down vote up
private Collector<TypeElement, ?, Set<TypeElement>> toSet() {
  return Collectors.toCollection(LinkedHashSet::new);
}
 
Example 16
Source File: TreeBackedRoundEnvironment.java    From buck with Apache License 2.0 4 votes vote down vote up
private Collector<Element, ?, Set<Element>> toSet() {
  return Collectors.toCollection(LinkedHashSet::new);
}