java8-stream练习-求3个班级的男生的平均身高中-最高的那个身高

  1. 1. 问题
  2. 2. 代码
    1. 3. 小结:

1. 问题

川哥在群里出的题:

一共三个班级,每个班级的人数(男生,女生)不等,要求求出这三个班级的男生的平均身高中,最高的那个身高

2. 代码

package com.niewj.stream;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
 * 求3个班级(人数/男生/女生不等)的男生的平均身高中,最高的那个身高
 * <p>
 * Created by niewj on 2020/9/24 11:40 */public class StreamTest {
    public static void main(String[] args) {
        // 初始化3个年级
 Grade grade1 = new Grade("1年级");
        Grade grade2 = new Grade("2年级");
        Grade grade3 = new Grade("3年级");
        // 初始化数据 24个学生
 List<Student> studentList = initStudents(grade1, grade2, grade3);
        // 答题
 Optional<Double> max = studentList.stream()
                .filter(s -> "男".equals(s.getGender()))
                .collect(Collectors.groupingBy(Student::getGrade, Collectors.averagingDouble(Student::getHeight)))
                .values()
                .stream()
                .max(Double::compareTo);
        System.out.println(max.orElse(0.0));
    }
    private static List<Student> initStudents(Grade grade1, Grade grade2, Grade grade3) {
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student(grade1, "女", "张00", 160));
        studentList.add(new Student(grade2, "男", "张01", 179));
        studentList.add(new Student(grade3, "男", "张02", 172));
        studentList.add(new Student(grade1, "女", "张03", 162));
        studentList.add(new Student(grade2, "男", "张04", 170));
        studentList.add(new Student(grade3, "男", "张05", 162));
        studentList.add(new Student(grade2, "男", "张06", 175));
        studentList.add(new Student(grade1, "女", "张07", 162));
        studentList.add(new Student(grade2, "男", "张08", 172));
        studentList.add(new Student(grade3, "男", "张09", 176));
        studentList.add(new Student(grade2, "女", "张10", 159));
        studentList.add(new Student(grade1, "男", "张11", 172));
        studentList.add(new Student(grade2, "男", "张12", 177));
        studentList.add(new Student(grade3, "女", "张13", 170));
        studentList.add(new Student(grade2, "男", "张14", 172));
        studentList.add(new Student(grade1, "女", "张15", 166));
        studentList.add(new Student(grade2, "女", "张16", 166));
        studentList.add(new Student(grade3, "男", "张17", 190));
        studentList.add(new Student(grade2, "女", "张18", 172));
        studentList.add(new Student(grade1, "女", "张19", 160));
        studentList.add(new Student(grade2, "女", "张20", 155));
        studentList.add(new Student(grade2, "男", "张21", 172));
        studentList.add(new Student(grade2, "男", "张22", 169));
        studentList.add(new Student(grade1, "女", "张23", 170));
        return studentList;
    }
    @Data
 @AllArgsConstructor @NoArgsConstructor static class Student {
        private Grade grade;
        private String gender;
        private String sname;
        private int height;
    }
    @Data
 @AllArgsConstructor static class Grade {
        private String className;
    }
}

3. 小结:

Optional<Double> max = studentList.stream()
        .filter(s -> "男".equals(s.getGender()))
        .collect(Collectors.groupingBy(Student::getGrade, Collectors.averagingDouble(Student::getHeight)))
        .values()
        .stream()
        .max(Double::compareTo);

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 hi@niewj.com

×

喜欢就点赞,疼爱就打赏