• 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 }
  • 相关阅读:
    迷宫与宝藏
    黑暗城堡
    Escaping the Farm
    Word Search
    描述
    防范sql注入值得注意地方
    fineui 实现下拉框模糊查询
    Fineui 根据datatable结构动态创建grid列,帮助类。动态绑定grid。
    Fineui 实现点击左边树状主菜单链接 打开新窗口或打开多个同一个tab
    fineui grid自定义选项框 带全选
  • 原文地址:https://www.cnblogs.com/kaililikai/p/5915612.html
Copyright © 2020-2023  润新知