文件读取数据后,进行合并和计算

 

map对象转java对象

list分类

optional处理

public static class Student{
public String getName() {
return name;
}

public String getSubject() {
return subject;
}

public Double getGrade() {
return grade;
}

String name;
String subject;
Double grade;
public Student(String name, String subject, Double grade){
this.name= name;
this.subject = subject;
this.grade = grade;
}


}
---------
下面是函数的具体实现:
FileInputStream fi = new FileInputStream("/Users/xx/workPlace/Test/a.txt");
InputStreamReader isr = new InputStreamReader(fi);
BufferedReader br = new BufferedReader(isr);
String line="";
List<Student> studentList = new ArrayList<>();

while
((line = br.readLine()) != null){ String[] data = line.split(" "); Student student = new Student(data[0], data[1], Double.valueOf(data[2])); studentList.add(student); } Map<String, List<Student>> stringListMap = studentList.stream().collect(Collectors.groupingBy(Student::getName)); stringListMap.forEach((k,v)->{ TestSu.Student st = v.stream().max((Comparator.comparing(Student::getGrade))).get(); Double avg = v.stream().mapToDouble(Student::getGrade).average().getAsDouble(); System.out.println(st.getName()+"最高分科目为:" + st.getSubject() + st.getGrade() + ",平均分=" + avg ); });

 

posted @ 2021-02-25 15:45  小叶子leavescy  阅读(99)  评论(0编辑  收藏  举报