0926-----homework
Posted 风林山火
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了0926-----homework相关的知识,希望对你有一定的参考价值。
1 /** 2 *homework0926 3 *@author:kai li 4 */ 5 package com.kai.li.homework0926; 6 import java.util.List; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.Map; 10 import static java.util.stream.Collectors.groupingBy; 11 /** 12 *following class is client 13 */ 14 public class HomeWork0926{ 15 public static void main(String[] args)throws Exception{ 16 17 /** 18 *question three 19 */ 20 21 /*create data source*/ 22 23 List<Student> students=Arrays.asList(new Student("小明",18,100,"class04"),new Student("小黄",22,70,"class04"),new Student("小真",25,90,"class03"),new Student("小花",30,80,"class02"),new Student("tom",28,66,"class02"),new Student("jerry",24,100,"class02")); 24 25 /*operate*/ 26 int sumAge=students.stream().mapToInt(Student::getAge).sum(); 27 int sumScore=students.stream().mapToInt(Student::getScore).sum(); 28 System.out.println("学生的平均年龄是:"+sumAge/students.size()+",平均分数是:"+sumScore/students.size()); 29 30 Map<String,List<Student>> studentsByClass=students.stream().collect(groupingBy(Student::getClassNum)); 31 System.out.println("class average score is fllowing:"); 32 33 studentsByClass.keySet().stream() //get stream 34 .mapToInt(i->(studentsByClass.get(i).stream() //map to int 35 .mapToInt(Student::getScore) //map to int from the level 2 36 .sum()) //sum 37 /studentsByClass.get(i).size()) //average 38 .forEach(System.out::println); //iterator and println 39 40 } 41 } 42 /** 43 *following class for question three 44 *create data source class 45 *student class 46 */ 47 class Student{ 48 private String name; 49 private int age; 50 private int score; 51 private String classNum; 52 Student(String name,int age,int score,String classNum){ 53 this.name=name; 54 this.age=age; 55 this.score=score; 56 this.classNum=classNum; 57 } 58 public String getName(){ 59 return name; 60 } 61 public int getAge(){ 62 return age; 63 } 64 public int getScore(){ 65 return score; 66 } 67 public String getClassNum(){ 68 return classNum; 69 } 70 }
以上是关于0926-----homework的主要内容,如果未能解决你的问题,请参考以下文章