Example usage for java.util.stream Collectors groupingBy

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

Introduction

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

Prototype

public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
        Collector<? super T, A, D> downstream) 

Source Link

Document

Returns a Collector implementing a cascaded "group by" operation on input elements of type T , grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector .

Usage

From source file:com.gs.collections.impl.jmh.AggregateByTest.java

@Warmup(iterations = 20)
@Measurement(iterations = 10)// www .  j  ava2s. c o  m
@Benchmark
public Map<Account, DoubleSummaryStatistics> aggregateByAccount_serial_lazy_jdk() {
    Map<Account, DoubleSummaryStatistics> accountDoubleMap = this.jdkPositions.stream().collect(Collectors
            .groupingBy(Position::getAccount, Collectors.summarizingDouble(Position::getMarketValue)));
    Assert.assertNotNull(accountDoubleMap);
    return accountDoubleMap;
}

From source file:io.mandrel.metrics.impl.MongoMetricsRepository.java

@Override
public void sync(Map<String, Long> accumulators) {

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime keytime = now.withMinute(0).withSecond(0).withNano(0);

    // {global.XXX=0, global.YYY=0, ...} to {global{XXX=O, YYY=0}, ...}
    Stream<Pair<String, Pair<String, Long>>> map = accumulators.entrySet().stream().map(e -> {
        Iterable<String> results = splitter.split(e.getKey());
        List<String> elts = Lists.newArrayList(results);
        return Pair.of(elts.get(0), Pair.of(elts.get(1), e.getValue()));
    });/*from   w w w  . ja  v  a2 s  .  c o  m*/
    Map<String, List<Pair<String, Long>>> byKey = map.collect(Collectors.groupingBy(e -> e.getLeft(),
            Collectors.mapping(e -> e.getRight(), Collectors.toList())));

    List<? extends WriteModel<Document>> requests = byKey.entrySet().stream().map(e -> {
        Document updates = new Document();

        e.getValue().stream().forEach(i -> {
            Iterable<String> results = splitter.split(i.getKey());
            List<String> elts = Lists.newArrayList(results);
            if (elts.size() > 1) {
                updates.put(elts.get(0) + "." + JsonBsonCodec.toBson(elts.get(1)), i.getValue());
            } else {
                updates.put(i.getKey(), i.getValue());
            }
        });

        return new UpdateOneModel<Document>(Filters.eq("_id", e.getKey()), new Document("$inc", updates),
                new UpdateOptions().upsert(true));
    }).collect(Collectors.toList());

    counters.bulkWrite(requests);

    requests = byKey.entrySet().stream().map(e -> {
        List<UpdateOneModel<Document>> tsUpdates = Lists.newArrayList();

        e.getValue().stream().forEach(i -> {
            Iterable<String> results = splitter.split(i.getKey());
            List<String> elts = Lists.newArrayList(results);

            if (elts.size() == 1 && e.getKey().equalsIgnoreCase(MetricKeys.global())) {
                tsUpdates.add(new UpdateOneModel<Document>(
                        Filters.and(Filters.eq("type", e.getKey() + MetricKeys.METRIC_DELIM + i.getKey()),
                                Filters.eq("timestamp_hour", keytime)),
                        new Document("$inc",
                                new Document("values." + Integer.toString(now.getMinute()), i.getValue())),
                        new UpdateOptions().upsert(true)));
            }
        });

        return tsUpdates;
    }).flatMap(list -> list.stream()).collect(Collectors.toList());

    timeseries.bulkWrite(requests);

}

From source file:com.gs.collections.impl.jmh.AggregateByTest.java

@Warmup(iterations = 20)
@Measurement(iterations = 10)//from   ww w . j  a va  2 s  . c  o m
@Benchmark
public Map<String, DoubleSummaryStatistics> aggregateByCategory_serial_lazy_jdk() {
    Map<String, DoubleSummaryStatistics> categoryDoubleMap = this.jdkPositions.stream().collect(Collectors
            .groupingBy(Position::getCategory, Collectors.summarizingDouble(Position::getMarketValue)));
    Assert.assertNotNull(categoryDoubleMap);
    return categoryDoubleMap;
}

From source file:com.epam.dlab.module.aws.S3FileList.java

/**
 * Returns list of files that per billing period
 * For particular billing period file with the biggest modification date will be returned
 *
 * @param objectSummaries amazon s3 objects
 * @return list of file names/* w  w  w  .j  a  va  2 s.c  o  m*/
 */
protected List<String> lastFilesPerBillingPeriod(List<S3ObjectSummary> objectSummaries) {
    final Map<String, List<S3ObjectSummary>> months = objectSummaries.stream().collect(
            Collectors.groupingBy(o -> extractDatePrefix(reportPattern, o), mapping(o -> o, toList())));

    return months.entrySet().stream().flatMap(this::lastFileForBillingPeriod).sorted()
            .collect(Collectors.toList());
}

From source file:com.gs.collections.impl.jmh.AggregateByTest.java

@Warmup(iterations = 20)
@Measurement(iterations = 10)/*from  w ww  .j  a  va  2 s .  c  om*/
@Benchmark
public Map<Product, DoubleSummaryStatistics> aggregateByProduct_parallel_lazy_jdk() {
    Map<Product, DoubleSummaryStatistics> result = this.jdkPositions.parallelStream().collect(Collectors
            .groupingBy(Position::getProduct, Collectors.summarizingDouble(Position::getMarketValue)));
    Assert.assertNotNull(result);
    return result;
}

From source file:hr.diskobolos.service.impl.DashboardServiceImpl.java

private DashboardDto.GroupsOfCategorization fetchGroupsOfCategorization(
        Map<MemberRegister, Integer> categorizationTotalPointsPerMemberRegister) {

    int totalPoints = categorizationTotalPointsPerMemberRegister.entrySet().stream()
            .collect(Collectors.summingInt(c -> c.getValue()));

    Map<IDashboardDto.CategorizationOfSportsGroup, Integer> categorizationOfSportsGroups = categorizationTotalPointsPerMemberRegister
            .entrySet().stream().collect(Collectors.groupingBy(t -> {
                int value = t.getValue();
                IDashboardDto.CategorizationOfSportsGroup categorizationOfSportsGroup = null;
                if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1;
                } else if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2;
                } else if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3;
                } else if (IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4.getFrom() <= value
                        && value <= IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4.getTo()) {
                    categorizationOfSportsGroup = IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4;
                }/*  w w w  .  ja va  2s  .c o m*/
                return categorizationOfSportsGroup;
            }, Collectors.summingInt(t -> t.getValue())));

    DashboardDto.GroupsOfCategorization groupsOfCategorization = new DashboardDto.GroupsOfCategorization();
    groupsOfCategorization.setNumberOfMembersFirstCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_1)));
    groupsOfCategorization.setNumberOfMembersSecondCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_2)));
    groupsOfCategorization.setNumberOfMembersThirdCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_3)));
    groupsOfCategorization.setNumberOfMembersFourthCategory(calculatePercentage(totalPoints,
            categorizationOfSportsGroups.get(IDashboardDto.CategorizationOfSportsGroup.CATEGORY_4)));
    return groupsOfCategorization;
}

From source file:nu.yona.server.analysis.service.ActivityService.java

private void createInactivityEntities(Set<IntervalInactivityDto> missingInactivities) {
    Map<UUID, Set<IntervalInactivityDto>> activitiesByUserAnonymizedId = missingInactivities.stream()
            .collect(Collectors.groupingBy(mia -> mia.getUserAnonymizedId().get(), Collectors.toSet()));

    activitiesByUserAnonymizedId/*w  ww  . ja  v  a 2  s. com*/
            .forEach((u, mias) -> analysisEngineProxyService.createInactivityEntities(u, mias));
}

From source file:com.gs.collections.impl.jmh.AggregateByTest.java

@Warmup(iterations = 20)
@Measurement(iterations = 10)//from   ww w . j a va2 s  . c om
@Benchmark
public Map<Account, DoubleSummaryStatistics> aggregateByAccount_parallel_lazy_jdk() {
    Map<Account, DoubleSummaryStatistics> result = this.jdkPositions.parallelStream().collect(Collectors
            .groupingBy(Position::getAccount, Collectors.summarizingDouble(Position::getMarketValue)));
    Assert.assertNotNull(result);
    return result;
}

From source file:com.firewallid.termcloud.TermCloud.java

public void saveTermCloudAll(JavaPairRDD<String, List<Tuple2<String, Double>>> doc, String fileNamePrefix)
        throws IOException {
    List<Tuple2<String, List<Tuple2<String, Double>>>> collectDoc = doc.collect();

    if (collectDoc.isEmpty()) {
        return;/*from   w w  w .  j  a v a  2  s .c  o  m*/
    }

    /* Reduced feature-value list */
    List<Tuple2<String, Double>> featureValueList = collectDoc.parallelStream()
            .map(titleFeatures -> titleFeatures._2).reduce((featureValueList1, featureValueList2) -> {
                List<Tuple2<String, Double>> combineList = FIUtils.combineList(featureValueList1,
                        featureValueList2);

                List<Tuple2<String, Double>> collect = combineList.parallelStream()
                        .collect(Collectors
                                .groupingBy(t -> t._1, Collectors.mapping(t -> t._2, Collectors.toList())))
                        .entrySet().parallelStream()
                        .map(t -> new Tuple2<String, Double>(t.getKey(),
                                t.getValue().parallelStream().mapToDouble(Double::doubleValue).sum()))
                        .collect(Collectors.toList());

                return collect;
            }).get();

    /* Sorting */
    List<Tuple2<String, Double>> featureValueListSorted = FIUtils.sortDescTupleListByValue(featureValueList);

    /* Top N */
    List<Tuple2<String, Double>> featureValueListTopN;
    if (featureValueListSorted.size() <= conf.getInt(TOPN, 100)) {
        featureValueListTopN = new ArrayList<>(featureValueListSorted);
    } else {
        featureValueListTopN = new ArrayList<>(featureValueListSorted.subList(0, conf.getInt(TOPN, 100)));
    }

    /* Text for file. One line, one feature-value pair */
    String featureValueText = featureValueListTopN.parallelStream()
            .map(feature -> feature._1 + StringEscapeUtils.unescapeJava(conf.get(LINE_DELIMITER)) + feature._2)
            .collect(Collectors.joining(System.lineSeparator()));

    /* Save to file */
    FIFile.writeStringToHDFSFile(FIFile.generateFullPath(conf.get(TERMCLOUD_FOLDER),
            createFileNameTermCloud(fileNamePrefix, conf.get(ALLNAME))), featureValueText);
}

From source file:com.gs.collections.impl.jmh.AggregateByTest.java

@Warmup(iterations = 20)
@Measurement(iterations = 10)// w w w  .j ava2  s.c om
@Benchmark
public Map<String, DoubleSummaryStatistics> aggregateByCategory_parallel_lazy_jdk() {
    Map<String, DoubleSummaryStatistics> result = this.jdkPositions.parallelStream().collect(Collectors
            .groupingBy(Position::getCategory, Collectors.summarizingDouble(Position::getMarketValue)));
    Assert.assertNotNull(result);
    return result;
}