java 8 新特性 Collectors 自定义扩展 BigDecimal类型

package com.ziku.msp.canyin.order.mapper;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

/**
* @author Asher
* @date 2018/8/9 22:25
*/
public class CollectorsUtils {

static final Set CH_NOID = Collections.emptySet();

private CollectorsUtils() {
}

@SuppressWarnings("unchecked")
private static  Function castingIdentity() {
    return i -> (R) i;
}

/**
 * Simple implementation class for {@code Collector}.
 *
 * @param 
 *            the type of elements to be collected
 * @param 
 *            the type of the result
 */
static class CollectorImpl implements Collector {
    private final Supplier supplier;
    private final BiConsumer accumulator;
    private final BinaryOperator combiner;
    private final Function finisher;
    private final Set characteristics;

    CollectorImpl(Supplier supplier, BiConsumer accumulator, BinaryOperator combiner,
                  Function finisher, Set characteristics) {
        this.supplier = supplier;
        this.accumulator = accumulator;
        this.combiner = combiner;
        this.finisher = finisher;
        this.characteristics = characteristics;
    }

    CollectorImpl(Supplier supplier, BiConsumer accumulator, BinaryOperator combiner,
                  Set characteristics) {
        this(supplier, accumulator, combiner, castingIdentity(), characteristics);
    }

    @Override
    public BiConsumer accumulator() {
        return accumulator;
    }

    @Override
    public Supplier supplier() {
        return supplier;
    }

    @Override
    public BinaryOperator combiner() {
        return combiner;
    }

    @Override
    public Function finisher() {
        return finisher;
    }

    @Override
    public Set characteristics() {
        return characteristics;
    }
}

public static  Collector summingBigDecimal(ToBigDecimalFunction mapper) {
    return new CollectorImpl<>(() -> new BigDecimal[1], (a, t) -> {
        if (a[0] == null) {
            a[0] = BigDecimal.ZERO;
        }
        a[0] = a[0].add(mapper.applyAsBigDecimal(t));
    }, (a, b) -> {
        a[0] = a[0].add(b[0]);
        return a;
    }, a -> a[0], CH_NOID);
}

}

package com.ziku.msp.canyin.order.mapper;

import org.apache.poi.ss.formula.functions.T;

import java.math.BigDecimal;

/**
* @author Asher
* @date 2018/8/9 22:18
*/
@FunctionalInterface
public interface ToBigDecimalFunction {

/**
 * Applies this function to the given argument.
 *
 * @param value
 *            the function argument
 * @return the function result
 */
BigDecimal applyAsBigDecimal(T value);

}

List nowOrderStatis = orderStatisMapper.selectByExample(nowOrderStatisExample);
// 分组

    Map bigDecimalMap = nowOrderStatis.stream()
            .collect(Collectors.groupingBy(OrderStatis::getStoreName
                    ,CollectorsUtils.summingBigDecimal(OrderStatis::getPaidUpAmount)
                   ));
    // 排序
    bigDecimalMap.entrySet().stream().sorted(Map.Entry.comparingByValue().reversed())
            .forEachOrdered(System.out::println);
    System.out.println("listMap:"+ JSON.toJSONString(bigDecimalMap));
    // 遍历map,求出当天记录的条数

// HashMap

你可能感兴趣的:(java应用)