Example usage for java.util.stream Collectors groupingByConcurrent

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

Introduction

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

Prototype

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

Source Link

Document

Returns a concurrent 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:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, Long> countByGender = Employee.persons().stream()
            .collect(Collectors.groupingByConcurrent(Employee::getGender, Collectors.counting()));
    System.out.println(countByGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, String> namesByGender = Employee.persons().stream()
            .collect(Collectors.groupingByConcurrent(Employee::getGender,
                    Collectors.mapping(Employee::getName, Collectors.joining(", "))));
    System.out.println(namesByGender);
}

From source file:com.baidu.rigel.biplatform.tesseract.isservice.search.agg.AggregateCompute.java

/**
 * ?/*from  w ww. j  a v  a2  s. c  om*/
 * 
 * @param dataList
 *            ?
 * @param query
 *            
 * @return LinkedList<ResultRecord> ??
 */
public static List<SearchIndexResultRecord> aggregate(List<SearchIndexResultRecord> dataList, int dimSize,
        List<QueryMeasure> queryMeasures) {

    if (CollectionUtils.isEmpty(queryMeasures) || CollectionUtils.isEmpty(dataList) || dataList.size() == 1) {
        LOGGER.info("no need to group.");
        return dataList;
    }
    List<SearchIndexResultRecord> result = new ArrayList<SearchIndexResultRecord>();

    //        Set<Integer> countIndex = Sets.newHashSet();
    //        for (int i = 0 ; i < queryMeasures.size() ; i++) {
    //            if (queryMeasures.get(i).getAggregator().equals(Aggregator.COUNT)) {
    //                countIndex.add(dimSize + i);
    //            }
    //        }
    int arraySize = dataList.get(0).getFieldArraySize();

    long current = System.currentTimeMillis();
    Map<String, SearchIndexResultRecord> groupResult = dataList.parallelStream()
            .collect(Collectors.groupingByConcurrent(SearchIndexResultRecord::getGroupBy, Collectors
                    .reducing(new SearchIndexResultRecord(new Serializable[arraySize], null), (x, y) -> {
                        SearchIndexResultRecord var = new SearchIndexResultRecord(new Serializable[arraySize],
                                y.getGroupBy());
                        try {
                            for (int i = 0; i < dimSize; i++) {
                                var.setField(i, y.getField(i));
                            }
                            int index = dimSize;
                            for (int i = 0; i < queryMeasures.size(); i++) {
                                QueryMeasure measure = queryMeasures.get(i);
                                index = i + dimSize;
                                var.setField(i + dimSize, Aggregate.aggregate(x.getField(index),
                                        y.getField(index), measure.getAggregator()));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            throw new RuntimeException(e);
                        }
                        return var;
                    })));
    LOGGER.info("group agg(sum) cost: {}ms!", (System.currentTimeMillis() - current));
    result.addAll(groupResult.values());

    return result;
}