• 0926-----homework(3)


     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 }
  • 相关阅读:
    cordova 日曆 聯系人 插件使用
    ionic 不同view的數據交互
    JQuery iframe 刷新效果
    fis学习
    如何用nfs命令烧写内核和文件系统(网络下载文件到nandflash)(未完)
    布线的基本原则
    电平转转换电路
    焊盘的制作
    flash-热风焊盘的制作
    焊盘的层面剖析
  • 原文地址:https://www.cnblogs.com/kaililikai/p/5915612.html
Copyright © 2020-2023  润新知