分拣存储02——高淇300讲笔记之分拣存储

  案例2的题目是这样的:定义一个Student类,属性:name姓名,no班号,score成绩。现在将若干Student对象放入List。请统计出每个班级的总分和平均分。

  思路非常简单,同样也是用分拣存储的思想:

  1.建一个学生类Student,里面有姓名、班级名称、成绩。

  2.建一个班级类ClassRoom,里面有班级名称、总分,还有一个List,用来放学生列表。

  3.把所有的学生对象放进一个List里,然后遍历这个List。在遍历List的时候进行分拣,定义一个HashMap,它的key里存班级名字,它的value里存班级对象

  4.通过迭代器遍历map,就可以获得每个班的总分和平均分了。

  先建一个学生类Student:

 1 package com.bjsxt.map;
 2 
 3 /**
 4  * 学生类
 5  *
 6  */
 7 public class Student {
 8     private String name;//姓名
 9     private String no;//班级
10     private double score;//成绩
11     
12     //无参构造器    
13     public Student() {
14         super();
15     }
16 
17     //有参构造器    
18     public Student(String name, String no, double score) {
19         super();
20         this.name = name;
21         this.no = no;
22         this.score = score;
23     }
24 
25     //以下是setter与getter方法    
26     public String getName() {
27         return name;
28     }
29 
30     public void setName(String name) {
31         this.name = name;
32     }
33 
34     public String getNo() {
35         return no;
36     }
37 
38     public void setNo(String no) {
39         this.no = no;
40     }
41 
42     public double getScore() {
43         return score;
44     }
45 
46     public void setScore(double score) {
47         this.score = score;
48     }
49     
50 }

  班级类ClassRoom:

 1 package com.bjsxt.map;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 /**
 7  * 一个班级 多个学生(学生列表)
 8  *
 9  */
10 public class ClassRoom {
11     private String no;//班级
12     private List<Student> stuList;//学生列表
13     private double total;//总分
14     
15     //无参构造器    
16     public ClassRoom() {
17         stuList = new ArrayList<Student>();
18     }
19     
20     //有参构造器
21     public ClassRoom(String no) {
22         this();
23         this.no= no;
24     }
25 
26     //有参构造器
27     public ClassRoom(String no, List<Student> stuList, double total) {
28         super();
29         this.no = no;
30         this.stuList = stuList;
31         this.total = total;
32     }
33 
34     //setter与getter方法
35     public String getNo() {
36         return no;
37     }
38 
39     public void setNo(String no) {
40         this.no = no;
41     }
42 
43     public List<Student> getStuList() {
44         return stuList;
45     }
46 
47     public void setStuList(List<Student> stuList) {
48         this.stuList = stuList;
49     }
50 
51     public double getTotal() {
52         return total;
53     }
54 
55     public void setTotal(double total) {
56         this.total = total;
57     }
58 
59     
60 }

  接下来才是我们真正进行分拣存储的地方:

 1 package com.bjsxt.map;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.Iterator;
 6 import java.util.List;
 7 import java.util.Map;
 8 import java.util.Set;
 9 
10 /**
11  * 定义一个Student类,属性:name姓名,no班号,score成绩。现在将若干Student对象放入List。请统计出每个班级的总分和平均分。
12  * 方案:面向对象 + 分拣存储
13  *
14  */
15 public class MapDemo03 {
16     public static void main(String[] args) {
17         //1.考试
18         List<Student> stuList = exam();
19         //2.分析成绩
20         Map<String,ClassRoom> map = count(stuList);
21         //3.查看成绩(总分 平均分)
22         view(map);
23     }
24     
25     /**
26      * 查看每个班的总分和平均分 --> 遍历map
27      */
28     public static void view(Map<String,ClassRoom> map) {
29         Set<String> keys = map.keySet();
30         //获取迭代器对象
31         Iterator<String> keysIt = keys.iterator();
32         //先判断
33         while(keysIt.hasNext()) {
34             //再获取
35             String no = keysIt.next();
36             ClassRoom room = map.get(no);
37             //查看总分 计算平均分
38             double total = room.getTotal();
39             double avg = total/room.getStuList().size();
40             System.out.println(no+"-->"+total+"-->"+avg);
41         }
42     }
43     
44     /**
45      * 统计分析
46      * 1.面向对象
47      * 2.分拣存储
48      */
49     public static Map<String,ClassRoom> count(List<Student> list) {
50         Map<String,ClassRoom> map = new HashMap<String,ClassRoom>();
51         //1.遍历List
52         for(Student stu:list) {
53             //2.分拣 查看是否存在该编号的班级
54             String no = stu.getNo();  //班级编号
55             double score = stu.getScore();  //该学生的分数
56             //如果不存在,创建班级
57             ClassRoom room = map.get(no);
58             if(null == room) {
59                 room = new ClassRoom(no);
60                 map.put(no, room);
61             }
62             //存在,放入学生
63             room.getStuList().add(stu);
64             room.setTotal(room.getTotal()+score);  //计算总分
65         }
66         
67         return map;
68     }
69     
70     /**
71      * 模拟考试 测试数据 到List中
72      */
73     public static List<Student> exam() {
74         List<Student> list = new ArrayList<Student>();
75         //存放学生成绩
76         list.add(new Student("老裴","a",85));
77         list.add(new Student("裴兜兜","a",86));
78         list.add(new Student("裴裴","a",89));
79         list.add(new Student("高小三","b",80));
80         list.add(new Student("高高","b",80));
81         return list;
82     }
83     
84 }

  

原文地址:https://www.cnblogs.com/swimminglover/p/8319475.html