zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Angularjs中的ng-class
    AngularJS 的表单验证
    Eclipse更新慢、插件安装慢解决方案zz
    PSD的单位及计算方法[转]
    .NET控件名称缩写一览表 zz
    C#Stopwatch的简单计时zz
    VsVim的快捷键
    MySQL-mysql 8.0.11安装教程
    使用open live writer客户端写博客zz
    WPFToolkit DataGrid 使用介绍zz
  • 原文地址:https://www.cnblogs.com/kaililikai/p/5915612.html
Copyright © 2011-2022 走看看