hash和list的运用

来源:互联网 发布:java udp函数 编辑:程序博客网 时间:2024/05/17 21:29
利用Map和List统计各班的总分和人数
面试


package list.map;

public class Student {
    private String name;
    private String number;
    private double score;
    
    public Student() {}
    public Student(String name,String number,double score) {
        this.name = name;
        this.number = number;
        this.score = score;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getNumber() {
        return this.number;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public double getScore() {
        return this.score;
    }
}


package list.map;

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

public class ClassRoom {
    private String number;
    private double total;
    private List list = null;
   
    public ClassRoom() {
        list = new ArrayList();
    }
    public ClassRoom(String number,double total) {
        this();
        this.number = number;
        this.total = total;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getNumber() {
        return this.number;
    }
    public void setTotal(double total) {
        this.total = total;
    }
    public double getTotal() {
        return this.total;
    }
   
    public void add(Student stu) {
        list.add(stu);
    }
    public List getList() {
        return this.list;
    }
}


package list.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;


public class AppMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List list = new ArrayList();
        add(list);
       
        Map map = new HashMap<>();
        count(map, list);
       
        print(map);
    }
   
    public static void add(List list) {
        list.add(new Student("a","网络151",10));
        list.add(new Student("b","网络151",10));
        list.add(new Student("c","网络152",10));
        list.add(new Student("d","网络153",10));
        list.add(new Student("e","网络153",10));
       
    }
   
    public static void count(Map map,List list) {
        for(Student stu : list) {
            if(!map.containsKey(stu.getNumber())) {
                ClassRoom cr = new ClassRoom(stu.getNumber(),stu.getScore());
                map.put(stu.getNumber(), cr);
                cr.add(stu);
            } else {
                ClassRoom cr = map.get(stu.getNumber());
                cr.setTotal(cr.getTotal() + stu.getScore());
                cr.add(stu);
            }
           
        }
    }
   
    public static void print(Map map) {
        Set> set = map.entrySet();
        Iterator> it = set.iterator();
        while(it.hasNext()) {
            Map.Entry entry = it.next();
            ClassRoom cr = entry.getValue();
            System.out.println(cr.getNumber() +" "+cr.getTotal());
            for(Student stu : cr.getList()) {
                System.out.print(stu.getName() +" ");
            }
            System.out.println();
        }
    }

}


网络152 10.0
c
网络151 20.0
a b
网络153 20.0
d e