stram- list统计(求和、最大、最小、平均)java集合
转载 小贺学习笔记
-
list.stream().mapToDouble(User::getHeight).sum()//和
-
list.stream().mapToDouble(User::getHeight).max()//最大
-
list.stream().mapToDouble(User::getHeight).min()//最小
-
list.stream().mapToDouble(User::getHeight).average()//平均值
当然,除了统计double类型,还有int和long,bigdecimal需要用到reduce求和
Double示例:
public class HelloWorld { private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点 public static void main(String[] args) { Random random = new Random(); List<User> list = new ArrayList<>(); for(int i=1;i<=5;i++) { double weight = random.nextDouble() * 100 + 100;//随机身高:100-200 User u = new User(i, "用户-" + i, weight); list.add(u); } System.out.println("用户:" + list); double sum = list.stream().mapToDouble(User::getHeight).sum(); System.out.println("身高 总和:" + df.format(sum)); double max = list.stream().mapToDouble(User::getHeight).max().getAsDouble(); System.out.println("身高 最大:" + df.format(max)); double min = list.stream().mapToDouble(User::getHeight).min().getAsDouble(); System.out.println("身高 最小:" + df.format(min)); double average = list.stream().mapToDouble(User::getHeight).average().getAsDouble(); System.out.println("身高 平均:" + df.format(average)); } private static class User{ Integer id; String name; double height;//身高 public User(Integer id, String name, double height) { this.id = id; this.name = name; this.height = height; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", height=" + height + '}'; } } }
BigDecimal示例:
public class HelloWorld { private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点 public static void main(String[] args) { Random random = new Random(); List<User> list = new ArrayList<>(); for(int i=1;i<=5;i++) { double weight = random.nextDouble() * 100 + 100;//随机身高:100-200 list.add(new User(i, new BigDecimal(weight).setScale(BigDecimal.ROUND_HALF_UP, 2))); } System.out.println("list:" + list); BigDecimal add = list.stream().map(User::getHeight).reduce(BigDecimal.ZERO, BigDecimal::add); System.out.println("身高 总和:" + df.format(add)); Optional<User> max = list.stream().max((u1, u2) -> u1.getHeight().compareTo(u2.getHeight())); System.out.println("身高 最大:" + df.format(max.get().getHeight())); Optional<User> min = list.stream().min((u1, u2) -> u1.getHeight().compareTo(u2.getHeight())); System.out.println("身高 最小:" + df.format(min.get().getHeight())); } private static class User{ Integer id; BigDecimal height;//身高 public User(Integer id, BigDecimal height) { this.id = id; this.height = height; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public BigDecimal getHeight() { return height; } public void setHeight(BigDecimal height) { this.height = height; } @Override public String toString() { return "User{" + "id=" + id + ", height=" + height + '}'; } } }
项目应用
/** * 超标率分析(点位分开查询)多条件筛选 */ @Override public Map<String, Object> getYcshAnalysis(Map<String, Object> map)throws Exception { Map<String, Object> maps = new HashMap<>(); Map<String, Object> Cbyzmap =new HashMap<>(); List<Ycsh> ycshs=ycshDao.getYcshAnalysis(map); //获取每个超标因的平均值 Cbyzmap.put("CO2JGavg", new BigDecimal(ycshs.stream().mapToDouble(Ycsh::getCO2JG).average().getAsDouble()).setScale(2,RoundingMode.HALF_UP)); Cbyzmap.put("HCJGavg", new BigDecimal(ycshs.stream().mapToDouble(Ycsh::getHCJG).average().getAsDouble()).setScale(2,RoundingMode.HALF_UP)); Cbyzmap.put("COJGavg", new BigDecimal(ycshs.stream().mapToDouble(Ycsh::getCOJG).average().getAsDouble()).setScale(2,RoundingMode.HALF_UP)); Cbyzmap.put("NOJGavg", new BigDecimal(ycshs.stream().mapToDouble(Ycsh::getNOJG).average().getAsDouble()).setScale(2,RoundingMode.HALF_UP));//获取每个车牌所对应的车辆数量 Map<String, Object> hpzl=this.getHPZLByCb(ycshs); maps.put("Cbyzmap", Cbyzmap); maps.put("hpzl", hpzl); //获取每个车牌颜色对应的车辆 maps.put("cpys", this.getClslByCpys(ycshs)); return maps; }
BigDecimal.setScale用法
BigDecimal.setScale()这个方法是用于格式化小数点
setScale(0)表示保留整数
setScale(1)表示保留一位小数,默认用四舍五入方式
setScale(1,BigDecimal.HALF_UP)四舍五入,2.35变成2.4
setScaler(1,BigDecimal.HALF_DOWN)四舍五入,2.35变成2.3,如果是5则向下舍
scale指的是你小数点后的位数。比如123.456则score就是3.
score()就是BigDecimal类中的方法
roundingMode是小数的保留模式。它们都是BigDecimal中的常量字段,有很多种
比如:BigDecimal.ROUND_HALF_UP表示的就是4舍5入