1. 程式人生 > >Java中Map的分揀儲存思想--2

Java中Map的分揀儲存思想--2

上面一個已經簡單的介紹了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);
    }
}