TriyHoo

做好一件事需要的是专注和持之以恒

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

  最近有学习了HashMap的经典用法,分拣策略存储。典型的应用为:有很多的班级的同学参加考试,求得每个班级的总分和平均分并输出。

  思路为:首先应用面向对象的思维建立Student类和ClassRoom类,Student类中有学生所在班级,学生姓名,学生成绩三个属性。ClassRoom类中有班级号(同学生的所在班级一致),班级总分,班级学生列表。通过建立俩个类在以后的操作数据中很方便。然后在主类中进行数据操作。

(1)第一个方法为exam(List<Student>)将所有的学生加入到list中,这样所有的学生信息可以在list中查询。

(2)第二个方法为count(Map<String,ClassRoom>,List<Student>),这个方法是分拣策略的核心,通过遍历list获得学生的班级号和成绩,然后检查map中是否存在这个班级,若不存在则新建一个班级将班级号和班级作为键值对存到map中去,若存在此班级,则直接修改班级的总分。

代码如下:

package csdreamlab.hashmapApplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class StuScore {
    
    public static void main(String[] args) {
        StuScore ss = new StuScore();
        List<Student> list = new ArrayList();
        ss.exam(list);
        Map<String,ClassRoom> map = new HashMap<String,ClassRoom>();
        ss.count(map, list);
        ss.printScore(map);
    }
    
    public void printScore(Map<String,ClassRoom> map) {
        Set<Map.Entry<String,ClassRoom>> set = map.entrySet();
        Iterator<Map.Entry<String,ClassRoom>> it = set.iterator();
        while(it.hasNext()) {
            Map.Entry<String, ClassRoom> entry = it.next();
            String no = entry.getKey();
            ClassRoom room = entry.getValue();
            System.out.println("班级" + no + "的总成绩为: " + room.getTotalScore() + ".");
        }
    }
    
    public void count(Map<String,ClassRoom> map,List<Student> list) {
        double tempScore = 0;
        for(Student stu : list) {
            String no = stu.getNo();
            double score = stu.getScore();
            ClassRoom room = map.get(no);
            if(room == null) {
                room = new ClassRoom(no);
                map.put(no, room);
            }
            room.getStu().add(stu);
            room.setTotalScore(room.getTotalScore()+score);
        }
    }
    
    public void exam(List<Student> list) {
        list.add(new Student("01" ,85, "jon"));
        list.add(new Student("01" ,94, "jen"));
        list.add(new Student("01" ,95, "vily"));
        list.add(new Student("02" ,86, "tim"));
        list.add(new Student("02" ,75, "tom"));
        list.add(new Student("02" ,65, "peter"));
        list.add(new Student("03" ,91, "siliy"));
        list.add(new Student("03" ,66, "goden"));
        list.add(new Student("04" ,83, "hill"));
        list.add(new Student("04" ,75, "wede"));
    }
}

学生类:

package csdreamlab.hashmapApplication;

public class Student {
    
    private String no;
    private double score;
    private String name;
    
    public Student(String no, double score, String name) {
        this.no = no;
        this.score = score;
        this.name = name;
    }
    
    public String getNo() {
        return no;
    }
    public void setNo(String no) {
        this.no = no;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    
}

班级类:

package csdreamlab.hashmapApplication;

import java.util.ArrayList;
import java.util.List;

public class ClassRoom {
    
    private String no;
    private double totalScore;
    private List<Student> stu;
    
    public ClassRoom(String no) {
        this();
        this.no = no;
    }
    
    public ClassRoom() {
        stu = new ArrayList();
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public double getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(double totalScore) {
        this.totalScore = totalScore;
    }

    public List<Student> getStu() {
        return stu;
    }

    public void setStu(List<Student> stu) {
        this.stu = stu;
    }
}

 

 

芝兰生于幽林,不以无人而不芳;

君子修道立德,不以穷困而改节;

posted on 2014-12-18 17:42  TriyHoo  阅读(80)  评论(0)    收藏  举报