码迷,mobileinhere.cn
首页 > 编程语言 > 详细

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

时间:2018-01-29 17:34:12      阅读:3995      评论:0      收藏:0      [点我收藏+]

标签:for   string   需要   png   sys   array   setname   list   random   

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

}

执行结果:

用户:
[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<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 +
                    ‘}‘;
        }
    }

}

执行结果:

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

 

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

标签:for   string   需要   png   sys   array   setname   list   random   

原文地址:www.cnblogs.com/yangzhenlong/p/8378029.html

(0)
(1)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
2014 mobileinhere.cn 版权所有 京icp备13008772号-2
华人娱乐注册