java.util.stream.Collectors#collectingAndThen ( )源码实例Demo

下面列出了java.util.stream.Collectors#collectingAndThen ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: crate   文件: JobLauncher.java
public List<CompletableFuture<Long>> executeBulk(TransactionContext txnCtx) {
    Iterable<NodeOperation> nodeOperations = nodeOperationTrees.stream()
        .flatMap(opTree -> opTree.nodeOperations().stream())
        ::iterator;
    Map<String, Collection<NodeOperation>> operationByServer = NodeOperationGrouper.groupByServer(nodeOperations);

    List<ExecutionPhase> handlerPhases = new ArrayList<>(nodeOperationTrees.size());
    List<RowConsumer> handlerConsumers = new ArrayList<>(nodeOperationTrees.size());
    List<CompletableFuture<Long>> results = new ArrayList<>(nodeOperationTrees.size());
    for (NodeOperationTree nodeOperationTree : nodeOperationTrees) {
        CollectingRowConsumer<?, Long> consumer = new CollectingRowConsumer<>(
            Collectors.collectingAndThen(Collectors.summingLong(r -> ((long) r.get(0))), sum -> sum));
        handlerConsumers.add(consumer);
        results.add(consumer.completionFuture());
        handlerPhases.add(nodeOperationTree.leaf());
    }
    try {
        setupTasks(txnCtx, operationByServer, handlerPhases, handlerConsumers);
    } catch (Throwable throwable) {
        return Collections.singletonList(CompletableFuture.failedFuture(throwable));
    }
    return results;
}
 
源代码2 项目: mug   文件: BiCollectors.java
/**
 * Returns a {@link BiCollector} that collects the key-value pairs into an immutable {@link Map}
 * using {@code valueCollector} to collect values of identical keys into a final value of type
 * {@code V}.
 *
 * <p>For example, the following calculates total population per state from city demographic data:
 *
 * <pre>{@code
 *  Map<StateId, Integer> statePopulations = BiStream.from(cities, City::getState, c -> c)
 *     .collect(toMap(summingInt(City::getPopulation)));
 * }</pre>
 *
 * <p>Entries are collected in encounter order.
 */
public static <K, V1, V> BiCollector<K, V1, Map<K, V>> toMap(Collector<V1, ?, V> valueCollector) {
  requireNonNull(valueCollector);
  return new BiCollector<K, V1, Map<K, V>>() {
    @Override
    public <E> Collector<E, ?, Map<K, V>> splitting(
        Function<E, K> toKey, Function<E, V1> toValue) {
      return Collectors.collectingAndThen(
          Collectors.groupingBy(
              toKey,
              LinkedHashMap::new, Collectors.mapping(toValue, valueCollector)),
          Collections::unmodifiableMap);
    }
  };
}
 
源代码3 项目: articles   文件: RandomCollectors.java
public static <T> Collector<T, ?, Stream<T>> toEagerShuffledStream() {
    return Collectors.collectingAndThen(
        toCollection(ArrayList::new),
        list -> {
            Collections.shuffle(list);
            return list.stream();
        });
}
 
源代码4 项目: spring-cloud-gcp   文件: PartTreeDatastoreQuery.java
/**
 * Based on the query options, returns a collector that puts Datastore query results
 * in a correct form.
 *
 * @return collector
 */
private Collector<?, ?, ?> getResultsCollector() {
	Collector<?, ?, ?> collector  = Collectors.toList();
	if (isCountingQuery && !PartTreeDatastoreQuery.this.tree.isDelete()) {
		collector = Collectors.counting();
	}
	else if (PartTreeDatastoreQuery.this.tree.isExistsProjection()) {
		collector = Collectors.collectingAndThen(Collectors.counting(), (count) -> count > 0);
	}
	return collector;
}
 
源代码5 项目: ignite   文件: IgniteCollectors.java
/**
 * Collector of {@link IgniteInternalFuture} inheritors stream to {@link GridCompoundFuture}.
 *
 * @param <T> Result type of inheritor {@link IgniteInternalFuture}.
 * @param <R> Result type of {@link GridCompoundFuture}.
 * @return Compound future that contains all stream futures
 *         and initialized with {@link GridCompoundFuture#markInitialized()}.
 */
public static <T, R> Collector<? super IgniteInternalFuture,
    ? super GridCompoundFuture<T, R>, GridCompoundFuture<T, R>> toCompoundFuture() {
    final GridCompoundFuture<T, R> res = new GridCompoundFuture<>();

    return Collectors.collectingAndThen(
        Collectors.reducing(
            res,
            res::add,
            (a, b) -> a // No needs to merge compound futures.
        ),
        GridCompoundFuture::markInitialized
    );
}
 
源代码6 项目: zheshiyigeniubidexiangmu   文件: StreamUtils.java
public static <T> Collector<T, ?, T> singletonCollector() {
  return Collectors.collectingAndThen(
      Collectors.toList(),
      list -> {
        if (list.size() > 1) {
          throw new IllegalStateException("List contains more than one element: " + list);
        }
        return list.size() > 0 ? list.get(0) : null;
      });
}
 
源代码7 项目: galeb   文件: ScheduledProducer.java
private Collector<Integer, ?, LinkedList<Integer>> shufflePages() {
    return Collectors.collectingAndThen(Collectors.toCollection(LinkedList::new),
        collected -> {
            Collections.shuffle(collected);
            return collected;
        });
}
 
源代码8 项目: Java-Coding-Problems   文件: Main.java
private static <T> Collector<T, ?, T[]> toArray(IntFunction<T[]> func) {
    return Collectors.collectingAndThen(
            Collectors.toList(), l -> l.toArray(func.apply(l.size())));
}
 
源代码9 项目: bisq-core   文件: SeedNodeAddresses.java
public static Collector<NodeAddress, ?, SeedNodeAddresses> collector() {
    return Collectors.collectingAndThen(Collectors.toSet(), SeedNodeAddresses::new);
}
 
源代码10 项目: jig   文件: FieldDeclarations.java
public static Collector<FieldDeclaration, ?, FieldDeclarations> collector() {
    return Collectors.collectingAndThen(Collectors.toList(), FieldDeclarations::new);
}
 
源代码11 项目: jig   文件: StaticFieldDeclarations.java
public static Collector<StaticFieldDeclaration, ?, StaticFieldDeclarations> collector() {
    return Collectors.collectingAndThen(Collectors.toList(), StaticFieldDeclarations::new);
}
 
源代码12 项目: jig   文件: MethodDeclarations.java
public static Collector<MethodDeclaration, ?, MethodDeclarations> collector() {
    return Collectors.collectingAndThen(toList(), MethodDeclarations::new);
}
 
源代码13 项目: jig   文件: TypeIdentifiers.java
public static Collector<TypeIdentifier, ?, TypeIdentifiers> collector() {
    return Collectors.collectingAndThen(Collectors.toList(), TypeIdentifiers::new);
}
 
源代码14 项目: selenium   文件: PersistentCapabilities.java
private <T> Collector<T, ?, Set<T>> toUnmodifiableSet() {
  return Collectors.collectingAndThen(Collectors.toSet(), Collections::unmodifiableSet);
}
 
源代码15 项目: cyclops   文件: LazySeq.java
static <T> Collector<T, List<T>, LazySeq<T>> collector() {
    Collector<T, ?, List<T>> c  = Collectors.toList();
    return Collectors.<T, List<T>, Iterable<T>,LazySeq<T>>collectingAndThen((Collector)c,LazySeq::fromIterable);
}
 
源代码16 项目: smithy   文件: MapUtils.java
/**
 * Creates a collector that collects into an unmodifiable Map.
 *
 * <p>This is a polyfill equivalent of Java 10's
 * {@code Collectors#toUnmodifiableMap}.
 *
 * @param <T> the type to retrieve keys and values from.
 * @param <K> the Map's key type.
 * @param <U> the Map's value type.
 * @param keyMapper Function that retrieves the key.
 * @param valueMapper Function that retrieves the value.
 * @return a Collector that accumulates the entries into an unmodifiable Map.
 */
@SuppressWarnings("unchecked")
public static <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(
        Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper
) {
    return Collectors.collectingAndThen(
            Collectors.toMap(
                    Objects.requireNonNull(keyMapper, "keyMapper"),
                    Objects.requireNonNull(valueMapper, "valueMapper")),
            Collections::unmodifiableMap);
}
 
源代码17 项目: Strata   文件: Guavate.java
/**
 * Collector used at the end of a stream to build an immutable list of
 * immutable lists of size equal to or less than size.
 * For example, the following list [a, b, c, d, e] with a partition
 * size of 2 will give [[a, b], [c, d], [e]].
 * <p>
 * A collector is used to gather data at the end of a stream operation.
 * This method returns a collector allowing streams to be gathered into
 * an {@link ImmutableList} of {@link ImmutableList}.
 *
 * @param size  the size of the partitions of the original list
 * @param <T>  the type of element in the list
 * @return the immutable list of lists collector
 */
public static <T> Collector<T, ?, ImmutableList<ImmutableList<T>>> splittingBySize(int size) {
  return Collectors.collectingAndThen(
      Collectors.collectingAndThen(
          Guavate.toImmutableList(),
          objects -> Lists.partition(objects, size)),
      Guavate::toImmutables);
}
 
源代码18 项目: cyclops   文件: SortedSetX.java
static <T> Collector<T, ?, SortedSet<T>> immutableCollector() {
    return Collectors.collectingAndThen(defaultCollector(), (final SortedSet<T> d) -> Collections.unmodifiableSortedSet(d));

}
 
源代码19 项目: mug   文件: MoreStreams.java
/**
 * Returns a collector that first copies all input elements into a new {@code Stream} and then
 * passes the stream to {@code toSink} function, which translates it to the final result.
 *
 * @since 3.6
 */
static <T, R> Collector<T, ?, R> copying(Function<Stream<T>, R> toSink) {
  return Collectors.collectingAndThen(toStream(), toSink);
}
 
源代码20 项目: streamex   文件: MoreCollectors.java
/**
 * Returns a {@code Collector} which counts a number of distinct values the
 * mapper function returns for the stream elements.
 * 
 * <p>
 * The operation performed by the returned collector is equivalent to
 * {@code stream.map(mapper).distinct().count()}. This collector is mostly
 * useful as a downstream collector.
 * 
 * @param <T> the type of the input elements
 * @param mapper a function which classifies input elements.
 * @return a collector which counts a number of distinct classes the mapper
 *         function returns for the stream elements.
 * @throws NullPointerException if mapper is null.
 */
public static <T> Collector<T, ?, Integer> distinctCount(Function<? super T, ?> mapper) {
    Objects.requireNonNull(mapper);
    return Collectors.collectingAndThen(Collectors.mapping(mapper, Collectors.toSet()), Set::size);
}