java8浮点型算平均值
实体类
public class Cs {private Float val;public Cs() {}public Cs(Float val) {this.val = val;}public Float getVal() {return val;}public void setVal(Float val) {this.val = val;}
}
JAVA8算平均值
List<Cs> list=new ArrayList<>();Cs cs1=new Cs(0.4f);Cs cs2=new Cs(0.5f);Cs cs3=new Cs(0.6f);Cs cs4=new Cs(0.2f);list.add(cs1);list.add(cs2);list.add(cs3);list.add(cs4);System.out.println(list.stream().mapToDouble(Cs::getVal).average().orElse(Double.NaN));
结果