java8 list统计(求和、最大、最小、平均)(转)

转载地址:https://www.cnblogs.com/yangzhenlong/p/8378029.html

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 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 +
                    '}';
        }
    }

}

 

执行结果:

 

用户:
    [User{id=1, name='用户-1', height=192.15677342306662}, 
     User{id=2, name='用户-2', height=196.35056058694772}, 
     User{id=3, name='用户-3', height=101.96271958293853}, 
     User{id=4, name='用户-4', height=110.83134063008366}, 
     User{id=5, name='用户-5', height=106.27720636757154}]
身高 总和:707.58
身高 最大:196.35
身高 最小:101.96
身高 平均:141.52

 

 

BigDecimal示例:

 

public class HelloWorld {
    private static final DecimalFormat df = new DecimalFormat("0.00");//保留两位小数点
    public static void main(String[] args) {
        Random random = new Random();
        List 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 max = list.stream().max((u1, u2) -> u1.getHeight().compareTo(u2.getHeight()));
        System.out.println("身高 最大:" + df.format(max.get().getHeight()));
        Optional 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 +
                    '}';
        }
    }

}

执行结果:

 

list:
    [User{id=1, height=141.5472}, 
    User{id=2, height=133.1609}, 
    User{id=3, height=101.5403}, 
    User{id=4, height=157.8470}, 
    User{id=5, height=177.7596}]
身高 总和:711.8550
身高 最大:177.76
身高 最小:101.54

科学记数法转Bigdecimal

public static String scientificNotation2String() {

String str="3e5";//30万

String value = null;

BigDecimal bd = new BigDecimal(str);

value = bd.toPlainString();

return value;

}

你可能感兴趣的:(java8 list统计(求和、最大、最小、平均)(转))