求集合中元素属性的最值

求集合中元素属性的最值,第1张

求集合中元素属性的最值 方法
 ArrayList studentArrayList = new ArrayList<>();
        studentArrayList.add(new Student("zhangsan",12,98,"优等生"));
        studentArrayList.add(new Student("lisi",13,75,"还不错"));
        studentArrayList.add(new Student("wangwu",14,58,"别上了"));
        studentArrayList.add(new Student("zaoliu",15,60,"就那样"));
        studentArrayList.add(new Student("tianqi",15,89,"还不错"));
        System.out.println(studentArrayList.stream().map(Student::getScore).max((x1, x2) ->x1.compareTo(x2)).get());
        System.out.println(studentArrayList.stream().map(Student::getScore).min((x1, x2) -> x1.compareTo(x2)).get());
方法二
在这里插入代码片ArrayList studentArrayList = new ArrayList<>();
        studentArrayList.add(new Student("zhangsan",12,98,"优等生"));
        studentArrayList.add(new Student("lisi",13,75,"还不错"));
        studentArrayList.add(new Student("wangwu",14,58,"别上了"));
        studentArrayList.add(new Student("zaoliu",15,60,"就那样"));
        studentArrayList.add(new Student("tianqi",15,89,"还不错"));
        Student max=studentArrayList.get(0);
        for (Student student : studentArrayList) {
            if (student.getScore()>max.getScore()){
                int score = student.getScore();
                max.setScore(score);
            }
        }
        System.out.println(max.getScore());

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5564123.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存