HashMap经典存储案例,分拣思路_与面向对象组合解题

本文介绍了一个使用Java实现的学生成绩统计程序。程序通过List存储多个Student对象,并利用HashMap统计每个班级的成绩总分及平均分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.pkushutong.Collection;

/**
 * 定义一个Student类,属性:名字、班号、成绩
 * 现在将若干Student对象放入List,请统计出每个班级的总分和平均分,分别打印出来
 */
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 Test09 {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		exam(list);
		
		//统计
		Map<String,ClassRoom> rooms = new HashMap<String, ClassRoom>();
		count(rooms,list);
		
		//打印
		printStudent(rooms);
	}
	
	//打印总分和平均分
	public static void printStudent(Map<String,ClassRoom> rooms){
		Set<Map.Entry<String, ClassRoom>> entrySet = rooms.entrySet();
		Iterator<Map.Entry<String, ClassRoom>> it = entrySet.iterator();
		while(it.hasNext()){
			Map.Entry<String, ClassRoom> entry = it.next();
			ClassRoom room = entry.getValue();
			double avg = room.getTotal()/room.getStu().size();
			System.out.println("班号为:"+room.getNo()+",总分:"+room.getTotal()+",平均分"+avg);
		}
	}
	
	//统计分数
	public static void count(Map<String,ClassRoom> rooms,List<Student> list){
		for(Student stu : list){
			String no = stu.getno();
			double score = stu.getScore();
			
			//根据班级编号 查看Map是否存在该班级
			ClassRoom room = rooms.get(no);
			if(room == null){	//第一次创建
				room = new ClassRoom(no);
				rooms.put(no, room);
			}
			//存储总分
			room.setTotal(room.getTotal()+score);
			room.getStu().add(stu);		//加入学生
		}
	}
	
	//现在将若干Student对象放入List
	public static void exam(List<Student> list){
		list.add(new Student("a","001",80));
		list.add(new Student("b","002",85));
		list.add(new Student("a","001",90));
		list.add(new Student("d","004",85));
	}
}

package com.pkushutong.Collection;

public class Student {
	private String name;
	private String no;
	private double score;
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", no=" + no
				+ ", score=" + score + "]";
	}
	public Student() {
	}
	
	public Student(String name, String no, double score) {
		super();
		this.name = name;
		this.no = no;
		this.score = score;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		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;
	}
	
	
}

package com.pkushutong.Collection;

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

/**
 * 班级
 * @author dell
 *
 */
public class ClassRoom {
	private String no;
	private List<Student> stu;	//学生
	private double total;		//总分
	
	public ClassRoom() {
		stu = new ArrayList<Student>();
	}
	
	public ClassRoom(String no) {
		this();
		this.no = no;
	}

	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public List<Student> getStu() {
		return stu;
	}
	public void setStu(List<Student> stu) {
		this.stu = stu;
	}
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
	
	
}


彩云 版本:mCloud2.2.0 | 大小:11 MB| 语言:中文 适用固件:Android 2.0及以上 分享日期:2013-12-13 游戏题材:商业 开发商:中国移动通信集团公司 内容介绍 彩云是中国移动旗下的个人网盘产品,面向所有用户提供安全、便捷、高效的个人云存储服务,帮助用户统一存储和管理不同终端的个人信息资产。用户首次注册即可获得16GB的网盘空间,通过官方推出的系列活动可免费领取T级空间。同时,依托运营商的资源优势,为用户提供业内速度最优的上传、下载体验。 【功能特点】 * 账号信息,安全无虞!——彩云向所有用户提供运营商级别的安全防护,除了免费登陆提醒,还有文件加密,全方位保障用户数字资产安全。 * T级空间,瞬间领取!——用户首次注册即可获得16GB的网盘空间,通过官方推出的系列活动可免费升级T级空间。 *精彩资源,短信分享!——直接转存他/她的彩云里,也可给他/她发短信,分享短信彩云无限量,免费发! * 跨端使用,就用彩云!——彩云为手机、个人电脑、平板电脑等提供的客户端覆盖所有操作系统,文件存彩云,手机、电脑,哪里想用点哪里。 * 手机信息,一键备份!——手机端可以实现通讯录、短彩信、手机应用、手机图片、手机视频和日历所有信息的全备份,信息丢失后只需一键恢复,即可找回所有数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值