Java基础之基于HashMap的分挑存储例子

2022-05-07 15:52:18 浏览数 (1)

按班级统计学生的平均成绩时,为了减少工作量,计划使用计算机来做这样的统计工作。

下面给出一个类Score用来表示每条考试成绩。

代码语言:javascript复制
package com.test;

/**
 * 学生成绩。
 */
class Score {
    /**
     * 学生姓名。
     */
    public String stdName;
    /**
     * 班级号。
     */
    public String classNumber;
    /**
     * 分数。
     */
    public int mark;
	public String getStdName() {
		return stdName;
	}
	public void setStdName(String stdName) {
		this.stdName = stdName;
	}
	public String getClassNumber() {
		return classNumber;
	}
	public void setClassNumber(String classNumber) {
		this.classNumber = classNumber;
	}
	public int getMark() {
		return mark;
	}
	public void setMark(int mark) {
		this.mark = mark;
	}
    
    
}

编写一个类ScoreStatistic,在类里实现以下方法:

代码语言:javascript复制
package com.test;

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 ScoreStatistic{
    public ScoreStatistic(){
    }
    public Map<String,Integer> statistic1(List<Score> scores){
		Map<String,Integer> classcountMap = new HashMap<String, Integer>();
		Map<String,Integer> classsumMap = new HashMap<String, Integer>();
		Iterator<Score> it = scores.iterator();
		while (it.hasNext()) {
			Score temp = it.next();
			String cn = temp.classNumber;
			int cm = temp.mark;
//			System.out.println(it.next().classNumber " " it.next().mark);
			if(classcountMap.get(cn)!=null){System.out.println("1");
				classcountMap.put(cn, classcountMap.get(cn) 1);
				classsumMap.put(cn, classsumMap.get(cn) cm);
			}else{
				classcountMap.put(cn, 1);
				classsumMap.put(cn,cm);
			}
			
		}
		Map<String, Integer> finalMap = new HashMap<String, Integer>();
		Iterator it1=classcountMap.keySet().iterator();    
		while(it1.hasNext()){    
		     String key;    
		     Integer value; 
		     Integer marksum;
		     key=it1.next().toString();    
		     value=classcountMap.get(key); 
		     marksum=classsumMap.get(key); System.out.println(value);
		     finalMap.put(key, marksum/value);
		     System.out.println(key ":" marksum/value);    
		}   
		return classsumMap;
	}
    
    public Map<String,Integer> statistic(List<Score> scores){
		Map<String,Integer> map = new HashMap<String,Integer>();
		
		Map<String,ClassRoom> roomMap = new HashMap<String,ClassRoom>();
		int avg;
		ClassRoom room = null;
		for(Score score:scores){
			String classNumber = score.getClassNumber();
			int mark = score.getMark();
			room = roomMap.get(classNumber); 
			if(room == null){
				room = new ClassRoom(classNumber);
				roomMap.put(classNumber, room);
			}
			room.setTotal(room.getTotal() mark);
			room.getList().add(score);
			//System.out.println(score.getClassNumber() ":" avg);
		}
		
		Set<Map.Entry<String,ClassRoom>> set = roomMap.entrySet();
		Iterator<Map.Entry<String, ClassRoom>> it = set.iterator();
		while(it.hasNext()){
			Map.Entry<String, ClassRoom> entry = it.next();
			ClassRoom ro = entry.getValue();
			avg = ro.getTotal()/ro.getList().size();
			map.put(ro.getClassNumber(), avg);
			System.out.println(ro.getClassNumber() ":" avg);
		}
		
		return map;
	}
    
    class ClassRoom{
    	/**
         * 班级号。
         */
        public String classNumber;
        
        /**
         * 总分
         */
        private int total;
        
        private List<Score> list;
        
        public ClassRoom(){
        	list = new ArrayList<Score>();
        }
        
        public ClassRoom(String classNumber){
        	this();
        	this.classNumber = classNumber;
        }

		public String getClassNumber() {
			return classNumber;
		}

		public void setClassNumber(String classNumber) {
			this.classNumber = classNumber;
		}

		public int getTotal() {
			return total;
		}

		public void setTotal(int total) {
			this.total = total;
		}

		public List<Score> getList() {
			return list;
		}

		public void setList(List<Score> list) {
			this.list = list;
		}
        
    }

}

0 人点赞