Example usage for java.util.stream Collectors collectingAndThen

List of usage examples for java.util.stream Collectors collectingAndThen

Introduction

In this page you can find the example usage for java.util.stream Collectors collectingAndThen.

Prototype

public static <T, A, R, RR> Collector<T, A, RR> collectingAndThen(Collector<T, A, R> downstream,
        Function<R, RR> finisher) 

Source Link

Document

Adapts a Collector to perform an additional finishing transformation.

Usage

From source file:Main.java

public static void main(String... args) {
    Map<Type, Food> o = Food.menu.stream().collect(Collectors.groupingBy(Food::getType,
            Collectors.collectingAndThen(
                    Collectors.reducing((Food d1, Food d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2),
                    Optional::get)));

    System.out.println(o);/*  w  ww. ja va2s . com*/

}

From source file:Main.java

public static void main(String[] args) {
    List<String> names = Employee.persons().stream().map(Employee::getName).collect(
            Collectors.collectingAndThen(Collectors.toList(), result -> Collections.unmodifiableList(result)));
    System.out.println(names);//from  ww  w .  jav  a2 s . c o m
}

From source file:Main.java

public static void main(String... args) {
    Map<Boolean, Food> o = Food.menu.stream().collect(Collectors.partitioningBy(Food::isVegetarian, Collectors
            .collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Food::getCalories)), Optional::get)));
    System.out.println(o);//from  ww w.ja v  a  2s  .  c  o m

}

From source file:Main.java

public static void main(String[] args) {
    Map<Month, String> dobCalendar = Employee.persons().stream()
            .collect(Collectors.collectingAndThen(Collectors.groupingBy(p -> p.getDob().getMonth(),
                    Collectors.mapping(Employee::getName, Collectors.joining(" "))), result -> {
                        for (Month m : Month.values()) {
                            result.putIfAbsent(m, "None");
                        }/*from   w ww  . j a  v a 2s  . c o m*/
                        return Collections.unmodifiableMap(new TreeMap<>(result));
                    }));

    dobCalendar.entrySet().forEach(System.out::println);
}

From source file:com.teradata.benchto.driver.macro.MacroServiceImpl.java

public void runBenchmarkMacro(String macroName, Optional<Benchmark> benchmark,
        Optional<Connection> connection) {
    MacroExecutionDriver macroExecutionDriver = macroExecutionDrivers.stream()
            .filter(executionDriver -> executionDriver.canExecuteBenchmarkMacro(macroName))
            .collect(Collectors.collectingAndThen(toList(), matchingExecutionDrivers -> {
                if (matchingExecutionDrivers.size() > 1) {
                    throw new IllegalStateException(
                            format("More than one execution driver for macro %s - matching drivers %s",
                                    macroName, matchingExecutionDrivers));
                } else if (matchingExecutionDrivers.size() == 0) {
                    throw new IllegalStateException(format("No execution driver for macro %s", macroName));
                }/*from   w ww  . j  a va2  s  .  c o m*/
                return matchingExecutionDrivers.get(0);
            }));

    macroExecutionDriver.runBenchmarkMacro(macroName, benchmark, connection);
}

From source file:cloud.api.gateway.ApiGatewayApplication.java

@Bean
UserInfoRestTemplateCustomizer userInfoRestTemplateCustomizer(SpringClientFactory springClientFactory) {
    return template -> {
        AccessTokenProviderChain accessTokenProviderChain = Stream
                .of(new AuthorizationCodeAccessTokenProvider(), new ImplicitAccessTokenProvider(),
                        new ResourceOwnerPasswordAccessTokenProvider(),
                        new ClientCredentialsAccessTokenProvider())
                .peek(tp -> tp.setRequestFactory(new RibbonClientHttpRequestFactory(springClientFactory)))
                .collect(Collectors.collectingAndThen(Collectors.toList(), AccessTokenProviderChain::new));
        template.setAccessTokenProvider(accessTokenProviderChain);
    };//from  w w  w.ja v  a 2s . c o m
}

From source file:com.epam.ta.reportportal.core.project.settings.impl.UpdateProjectSettingsHandler.java

@SuppressWarnings("unused")
private static <T> Collector<T, ?, T> findOneCollector(String id) {
    return Collectors.collectingAndThen(Collectors.toList(), shouldBeOne -> {
        if (shouldBeOne.size() != 1) {
            throw new ReportPortalException(String.format("No such issue sub-type found for id='%s'", id));
        }//w  ww.  j  a va2s.  com
        return shouldBeOne.get(0);
    });
}

From source file:com.ikanow.aleph2.graph.titan.utils.TitanGraphBuildingUtils.java

/** Separates out edges/vertices, groups by key
 * @param config//w ww .ja v  a2  s  . com
 * @param vertices_and_edges
 * @return
 */
protected static Map<ObjectNode, Tuple2<List<ObjectNode>, List<ObjectNode>>> groupNewEdgesAndVertices(
        final GraphSchemaBean config, final MutableStatsBean stats,
        final Stream<ObjectNode> vertices_and_edges) {
    final Map<ObjectNode, Tuple2<List<ObjectNode>, List<ObjectNode>>> nodes_to_get = vertices_and_edges
            .filter(o -> o.has(GraphAnnotationBean.type))
            .<Tuple3<ObjectNode, ObjectNode, Boolean>>flatMap(o -> {
                final JsonNode type = o.get(GraphAnnotationBean.type);

                if ((null == type) || !type.isTextual())
                    return Stream.empty();
                if (GraphAnnotationBean.ElementType.edge.toString().equals(type.asText())) {
                    stats.edges_emitted++;
                    // Grab both edges from both ends:
                    return Stream.concat(
                            Optional.ofNullable(o.get(GraphAnnotationBean.inV))
                                    .map(k -> Stream.of(Tuples._3T(convertToObject(k, config), o, false)))
                                    .orElse(Stream.empty()),
                            Optional.ofNullable(o.get(GraphAnnotationBean.outV))
                                    .map(k -> Stream.of(Tuples._3T(convertToObject(k, config), o, false)))
                                    .orElse(Stream.empty()));
                } else if (GraphAnnotationBean.ElementType.vertex.toString().equals(type.asText())) {
                    stats.vertices_emitted++;
                    return Optional.ofNullable(o.get(GraphAnnotationBean.id))
                            .map(k -> Stream.of(Tuples._3T(convertToObject(k, config), o, true)))
                            .orElse(Stream.empty());
                } else
                    return Stream.empty();
            }).collect(Collectors.groupingBy(t3 -> t3._1() // group by key
                    , Collectors.collectingAndThen(
                            Collectors.<Tuple3<ObjectNode, ObjectNode, Boolean>>partitioningBy(t3 -> t3._3()) // group by edge/vertex
                            ,
                            m -> Tuples._2T(m.get(true).stream().map(t3 -> t3._2()).collect(Collectors.toList()) // convert group edge/vertex to pair of lists
                                    , m.get(false).stream().map(t3 -> t3._2()).collect(Collectors.toList())))));

    return nodes_to_get;
}

From source file:org.jamocha.dn.compiler.pathblocks.PathBlocks.java

protected static <T, K, D> Collector<T, ?, Set<D>> groupingIntoSets(
        final Function<? super T, ? extends K> classifier, final Collector<? super T, ?, D> downstream) {
    final Collector<T, ?, Map<K, D>> groupingBy = groupingBy(classifier, downstream);
    return Collectors.collectingAndThen(groupingBy, map -> new HashSet<D>(map.values()));
}

From source file:org.jamocha.dn.compiler.pathblocks.PathBlocks.java

protected static PathBlockSet findAllMaximalBlocksInReducedScope(final Set<FilterInstance> filterInstances,
        final PathBlockSet resultBlocks) {
    final Iterable<List<FilterInstance>> filterInstancesGroupedByRule = filterInstances.stream()
            .collect(Collectors.collectingAndThen(groupingBy(FilterInstance::getRuleOrProxy), Map::values));
    final UndirectedGraph<FilterInstance, ConflictEdge> conflictGraph = determineConflictGraph(
            filterInstancesGroupedByRule);
    final Set<Set<Set<FilterInstance>>> filterInstancesGroupedByFilterAndByRule = filterInstances.stream()
            .collect(groupingIntoSets(FilterInstance::getFilter,
                    groupingIntoSets(FilterInstance::getRuleOrProxy, toSet())));
    for (final Set<Set<FilterInstance>> filterInstancesOfOneFilterGroupedByRule : filterInstancesGroupedByFilterAndByRule) {
        vertical(conflictGraph, filterInstancesOfOneFilterGroupedByRule, resultBlocks);
    }/*  w w w .  jav  a 2 s .  co m*/
    return resultBlocks;
}