Java8利⽤stream对list集合中的bigdecimal进⾏分组求和,均值,最⼤值,最⼩
值
Java8原⽣只提供了summingInt、summingLong、summingDouble三种基础类型的⽅法,想要对BigDecimal类型的数据操作需要⾃⼰新建⼯具类如下:
新建接⼝ToBigDecimalFunction
@FunctionalInterface
public interface ToBigDecimalFunction<T>{
BigDecimal applyAsBigDecimal(T value);
}
新建⼯具类CollectorsUtil
public class CollectorsUtil {
static final Set<Collector.Characteristics> CH_NOID = ptySet();
private CollectorsUtil(){
}
@SuppressWarnings("unchecked")
private static<I, R> Function<I, R>castingIdentity(){
return i ->(R) i;
}
static class CollectorImpl<T, A, R>implements Collector<T, A, R>{
private final Supplier<A> supplier;
private final BiConsumer<A, T> accumulator;
private final BinaryOperator<A> combiner;
private final Function<A, R> finisher;
private final Set<Characteristics> characteristics;
CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Function<A, R> finisher, Set<Characteristics> characteristics){
this.supplier = supplier;
this.accumulator = accumulator;
thisbiner = combiner;
this.finisher = finisher;
this.characteristics = characteristics;
}
CollectorImpl(Supplier<A> supplier, BiConsumer<A, T> accumulator, BinaryOperator<A> combiner,
Set<Characteristics> characteristics){
this(supplier, accumulator, combiner,castingIdentity(), characteristics);
}
@Override
public BiConsumer<A, T>accumulator(){
return accumulator;
}
@Override
public Supplier<A>supplier(){
return supplier;
}
@Override
public BinaryOperator<A>combiner(){
return combiner;
}
@Override
public Function<A, R>finisher(){
public Function<A, R>finisher(){
return finisher;
}
@Override
public Set<Characteristics>characteristics(){
return characteristics;
}
}
//求和⽅法
public static<T> Collector<T,?, BigDecimal>summingBigDecimal(ToBigDecimalFunction<?super T> mapper){
return new CollectorImpl<>(
()->new BigDecimal[]{BigDecimal.ZERO},
(a, t)->{ a[0]= a[0].add(mapper.applyAsBigDecimal(t));},
(a, b)->{ a[0]= a[0].add(b[0]);return a;},
a -> a[0], CH_NOID);
}
//求最⼤值
public static<T> Collector<T,?, BigDecimal>maxBy(ToBigDecimalFunction<?super T> mapper){
return new CollectorImpl<>(
()->new BigDecimal[]{new BigDecimal(Long.MIN_VALUE)},
(a, t)->{ a[0]= a[0].max(mapper.applyAsBigDecimal(t));},
(a, b)->{ a[0]= a[0].max(b[0]);return a;},
a -> a[0], CH_NOID);
}
//求最⼩值
public static<T> Collector<T,?, BigDecimal>minBy(ToBigDecimalFunction<?super T> mapper){
return new CollectorImpl<>(
()->new BigDecimal[]{new BigDecimal(Long.MAX_VALUE)},
(a, t)->{ a[0]= a[0].min(mapper.applyAsBigDecimal(t));},
(a, b)->{ a[0]= a[0].min(b[0]);return a;},
a -> a[0], CH_NOID);
}
//求平均值
public static<T> Collector<T,?, BigDecimal>averagingBigDecimal(ToBigDecimalFunction<?super T> mapper,int newScale,int roundingMode){ return new CollectorImpl<>(
()->new BigDecimal[]{BigDecimal.ZERO,BigDecimal.ZERO},
(a, t)->{ a[0]= a[0].add(mapper.applyAsBigDecimal(t)); a[1]= a[1].add(BigDecimal.ONE);},
(a, b)->{ a[0]= a[0].add(b[0]);return a;},
a -> a[0].divide(a[1],BigDecimal.ROUND_HALF_UP).setScale(newScale, roundingMode), CH_NOID);
}
}
新建实体类Person
@Data
class Person{
private String sex;
private Integer age;
private BigDecimal score;
public Person(String sex, Integer age, BigDecimal score){
this.sex = sex;
this.age = age;
this.score = score;
}
}
测试⽤例
List<Person> list =new ArrayList<>();
list.add(new Person("男",18,new BigDecimal(100)));
list.add(new Person("男",19,new BigDecimal(90)));
list.add(new Person("⼥",20,new BigDecimal(80)));
list.add(new Person("⼥",20,new BigDecimal(70)));
list.add(new Person("⼥",20,null));java stream
开始测试
/
/单条件筛选
//按照性别分组求分数总和
Map<String, BigDecimal> scoreCount = list.stream()
.filter(t -> t.getScore()!= null)
.upingBy(Person::getSex, CollectorsUtil.summingBigDecimal(Person::getScore))); System.out.println("----按照性别分组求分数总和----");
scoreCount.forEach((k,v)-> System.out.println("key: "+ k +" , "+"value: "+ v));
//按照性别求分数平均值
Map<String, BigDecimal> scoreAvg = list.stream()
.filter(t -> t.getScore()!= null)
.upingBy(Person::getSex, CollectorsUtil.averagingBigDecimal(Person::getScore,
2))); System.out.println("----按照性别求分数平均值----");
scoreAvg.forEach((k,v)-> System.out.println("key: "+ k +" , "+"value: "+ v));
//多条件筛选
//多条件筛选分组属性
private static String fetchGroupKey(Person p){
Age()+"#"+ p.getSex();
}
//按照性别年龄分组求分数总和
Map<String, BigDecimal> ageScoreCount = list.stream()
.filter(t -> t.getScore()!= null)
.upingBy(p ->fetchGroupKey(p), CollectorsUtil.summingBigDecimal(Person::get
Score))); System.out.println("----按照性别年龄分组求分数总和----");
ageScoreCount.forEach((k,v)-> System.out.println("key: "+ k +" , "+"value: "+ v));
//按照性别年龄分组求分数平均值
Map<String, BigDecimal> ageScoreAvg = list.stream()
.filter(t -> t.getScore()!= null)
.upingBy(p ->fetchGroupKey(p), CollectorsUtil.averagingBigDecimal(Person::getScore,2))); System.out.println("----按照性别年龄分组求分数平均值----");
ageScoreAvg.forEach((k,v)-> System.out.println("key: "+ k +" , "+"value: "+ v));
输出结果为
----按照性别分组求分数总和----
key:⼥, value:150
key:男, value:190
----按照性别求分数平均值----
key:⼥, value:75.00
key:男, value:95.00
----按照性别年龄分组求分数总和----
key:20#⼥, value:150
key:19#男, value:90
key:18#男, value:100
----按照性别年龄分组求分数平均值----
key:20#⼥, value:75.00
key:19#男, value:90.00
key:18#男, value:100.00
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论