自学JAVA-HashMap初级,练习代码,统计出每个班级的总分和平均分,分别打印出来
创建学生类
1package collenction;
2
3/**
4 * @author Administrator
5 *
6 */
7public class Student {
8
9 private String name;
10 private String classno;
11 private double score;
12
13
14 public Student(String name, String classno, double score) {
15 super();
16 this.name = name;
17 this.classno = classno;
18 this.score = score;
19 }
20
21 public String getName() {
22 return name;
23 }
24
25 public void setName(String name) {
26 this.name = name;
27 }
28
29 public double getScore() {
30 return score;
31 }
32
33 public void setScore(double score) {
34 this.score = score;
35 }
36
37 public String getClassno() {
38 return classno;
39 }
40
41 public void setClassno(String classno) {
42 this.classno = classno;
43 }
44
45}
创建班级类,用来存储学生
1package collenction;
2
3import java.util.ArrayList;
4import java.util.List;
5
6/**
7 * 班级
8 *
9 * @author Administrator
10 *
11 */
12public class ClassRoom {
13 private String no;
14 private List<Student> sut; // 学生列表
15 private double total; // 总分数
16
17 public ClassRoom() {
18 System.out.println("aslfjlsadk");
19 sut = new ArrayList<Student>();
20 }
21
22 public ClassRoom(String no) {
23 this.no = no;
24 sut = new ArrayList<Student>();
25 }
26
27 public String getNo() {
28 return no;
29 }
30
31 public void setNo(String no) {
32 this.no = no;
33 }
34
35 public List<Student> getSut() {
36 return sut;
37 }
38
39 public void setSut(List<Student> sut) {
40 this.sut = sut;
41 }
42
43 public double getTotal() {
44 return total;
45 }
46
47 public void setTotal(double total) {
48 this.total = total;
49 }
50
51}
首先需要初始化一些测试数据,学生
1public static void exam(List<Student> list){
2 list.add(new Student("张三","001",30));
3 list.add(new Student("张四","001",95));
4 list.add(new Student("张五","002",790));
5 list.add(new Student("张六","003",80));
6 list.add(new Student("张七","003",90));
7
8}
接着统计班级人数及分数
1public static void count(Map<String,ClassRoom> rooms,List<Student> list){
2 //循环处理所有的学生
3 for(Student stu:list) {
4 //获取学生信息
5 String no = stu.getClassno();
6 double score = stu.getScore();
7 ClassRoom room = rooms.get(no);
8 //判断该学生对应的班级是否存在
9 if(null == room) {
10 //班级不存在,创建一个,然后把学生放进来
11 room = new ClassRoom(no);
12 rooms.put(no, room);
13 }
14 //若班级存在,则把学生放进来
15 room.setTotal(room.getTotal()+score); //把当前学生的分数加到总分
16 room.getSut().add(stu);//先获取当前班级的学生list,然后把当前学生放进来
17 }
18}
最后打印总分和平均分
1public static void printScore(Map<String,ClassRoom> rooms) {
2 Set<Entry<String, ClassRoom>> entrySet = rooms.entrySet();//这里使用entrySet,不使用keySet,是因为entrySet会减少一次循环,效率更高
3 Iterator<Entry<String,ClassRoom>> it = entrySet.iterator();
4 while(it.hasNext()) {
5 Entry<String, ClassRoom> entry = it.next();
6 ClassRoom room = entry.getValue();
7 double avg = room.getTotal()/room.getSut().size();
8 //占位符 %s:字符串 %d:整数 %f:浮点 %b:boolean %%:表示一个%
9 System.out.printf("班级号为%s的班级共%s个学生,总分是%.2f分,平均分是%.2f \n" , room.getNo(),room.getSut().size(),room.getTotal(),avg);
10 }
11}
main
函数调用
1package collenction;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.Iterator;
6import java.util.List;
7import java.util.Map;
8import java.util.Map.Entry;
9import java.util.Set;
10
11public class HashMapTest02 {
12
13 public static void main(String[] args) {
14 //创建学生对象list
15 List<Student> list = new ArrayList<Student>();
16 //初始化一些学生
17 exam(list);
18
19 //创建班级
20 Map<String,ClassRoom> rooms = new HashMap<String, ClassRoom>();
21 //统计分数
22 count(rooms,list);
23
24 printScore(rooms);
25 }
26}
测试数据输出结果
班级号为001的班级共2个学生,总分是125.00分,平均分是62.50
班级号为002的班级共1个学生,总分是790.00分,平均分是790.00
班级号为003的班级共2个学生,总分是170.00分,平均分是85.00