上面一个已经简单的介绍了Map的分拣存储思想,下面通过一个复杂的实例来进一步理解分拣存储思想,虽然在百度的时候,百度分拣存储思想都是尚学堂的,可能我还不知道这个在实际中怎么去用吧。
案例2:定义一个Student类,属性:name 姓名,no班号,score 成绩 现在将若干Student对象放入List,请统计出每个班级的总分和平均分
//在解题之前,先建立两个对象,Student1类和Classroom1类
import java.util.ArrayList;
import java.util.List;
public class Classroom1 {
private String no;
private List<Student1> stu;
private double total;
public Classroom1() {
stu = new ArrayList();
}
public Classroom1(String no) {
this();
this.no = no;
}
public Classroom1(String no, List<Student1> stu, double total) {
super();
this.no = no;
this.stu = stu;
this.total = total;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public List<Student1> getStu() {
return stu;
}
public void setStu(List<Student1> stu) {
this.stu = stu;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
}
//在解题之前,先建立两个对象,Student1类和Classroom1类
public class Student1 {
private String name;
private String no;
private double score;
public Student1() {
}
public Student1(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;
}
@Override
public String toString() {
return this.name;
}
}
//推荐使用Map<String,Classroom1>来建立分拣存储的思想,这里为了操作的方便性,
//把学生对象的容器List放入Classroom1中
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 TestAverage_2 {
//将数据放入List中
public static List<Student1> exam(){
List<Student1> list = new ArrayList<>();
list.add(new Student1("老裴","a",85));
list.add(new Student1("裴兜兜","a",86));
list.add(new Student1("裴裴","a",89));
list.add(new Student1("高小三","b",80));
list.add(new Student1("高高","b",80));
return list;
}
//统计分析,也就是分拣存储,实现1:N的功能
public static Map<String,Classroom1> count(List<Student1> list){
Map<String,Classroom1> map = new HashMap<String,Classroom1>();
for(Student1 stu:list){
String no = stu.getNo();
double total = stu.getScore();
//查看有班级存在吗?如果没有的话就创建一个班级
Classroom1 room = map.get(no);
if(!map.containsKey(no)){
room = new Classroom1();
map.put(no, room);
}
//已经有班级存在了,剩下的就是把学生放在相应班级中
room.getStu().add(stu);
room.setTotal(room.getTotal()+total);
}
return map;
}
//计算班级的总分和平均分,也就是遍历Map
public static void view(Map<String,Classroom1> map ){
Set<String> set = map.keySet();
Iterator<String> it = set.iterator();
while(it.hasNext()){
String no = it.next();
double total = map.get(no).getTotal();
double avrage = total/(map.get(no).getStu().size());
System.out.println(no+"--"+total+"--"+avrage);
}
}
public static void main(String[] args) {
List<Student1> stu = exam();
Map<String,Classroom1> map = count(stu);
view(map);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.feng.test3.Student;
/**
* 不推荐使用Map<String,List<Student>> 操作不方便,这里只是想试试
* 直接用List当做Map的value值
*/
public class TestAverage {
//先将需要存储的对象放入List中
public static List<Student1> exam(){
List<Student1> list = new ArrayList<>();
list.add(new Student1("老裴","a",82));
list.add(new Student1("裴兜兜","a",82));
list.add(new Student1("裴裴","a",82));
list.add(new Student1("高小三","b",80));
list.add(new Student1("高高","b",80));
return list;
}
//实现分拣存储的思想,利用map实现1:N的映射,
//其中Key-->String 班级编号;Value-->对应着List<Student>
public static Map<String,List<Student1>> count(List<Student1> list){
Map<String,List<Student1>> map = new HashMap<>();
for(Student1 stu:list){
String no = stu.getNo();
if(!map.containsKey(no)){
List<Student1> value = new ArrayList<>();
map.put(no, value);
value.add(stu);
}else{
List<Student1> value = map.get(no);
value.add(stu);
}
}
return map;
}
public static void view(Map<String,List<Student1>> map){
double total = 0;
Set keyset = map.keySet();
Iterator<String> it = keyset.iterator();
while(it.hasNext()){
String no = it.next();
List<Student1> list = map.get(no);
System.out.println(no+list);
for(int i=0;i<list.size();i++){
Student1 stu = list.get(i);
total = total + stu.getScore();
}
double average = total/list.size();
System.out.println(no+"-->"+total+"-->"+average);
total = 0;
}
}
public static void main(String[] args) {
List<Student1> list = exam();
Map<String,List<Student1>> map = count(list);
view(map);
}
}