Example usage for java.util.stream Collectors reducing

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

Introduction

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

Prototype

public static <T> Collector<T, ?, T> reducing(T identity, BinaryOperator<T> op) 

Source Link

Document

Returns a Collector which performs a reduction of its input elements under a specified BinaryOperator using the provided identity.

Usage

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

/**
 * ?/*from  w  ww  . ja va 2s .c  o  m*/
 * 
 * @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;
}