新手上路之List,HashMap遍历二(1对多) 笔记总结

Student 类,用于单个学生的信息(1):

public class Student {
    // 单个学生
    // 姓名
    private String name;
    // 成绩 
    private Double score;
    // 课程
    private String courseEve;

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String name, Double score, String courseEve) {
        super();
        this.name = name;
        this.score = score;
        this.courseEve = courseEve;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getScore() {
        return score;
    }
    public void setScore(Double score) {
        this.score = score;
    }
    public String getCourseEve() {
        return courseEve;
    }
    public void setCourseEve(String courseEve) {
        this.courseEve = courseEve;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((courseEve == null) ? 0 : courseEve.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((score == null) ? 0 : score.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (courseEve == null) {
            if (other.courseEve != null)
                return false;
        } else if (!courseEve.equals(other.courseEve))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (score == null) {
            if (other.score != null)
                return false;
        } else if (!score.equals(other.score))
            return false;
        return true;
    }
}

学生列表(set容器添加),Course类(N):

import java.util.HashSet;
import java.util.Set;

public class Course {
    // 学生列表 
    // 課程名
    private String title;
    //存储学生类
    private Set<Student> students;
    // 统计总分数
    // 引用类型默认为null,加减 会出异常
    private Double total = 0.0;
    public Course() {
        students = new HashSet<>();
    }
    public Course(String title, Student stu) {
        this();
        this.title = title;
        this.students.add(stu);
        this.total += stu.getScore();
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    public Double getTotal() {
        return total;
    }
    public void setTotal(Double total) {
        this.total = total;
    }

}

实例代码TestStudentList 类:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TestStudentList {

    public static void main(String[] args) {
        // 实例一些学生的信息
        Student student = new Student("徐凤年", 99.0,"Java");
        Student student1 = new Student("徐胭脂", 89.5, "JavaEE");
        Student student2 = new Student("张还生", 65.4, "Java");
        Student student3 = new Student("徐凤年", 99.0,"c#");
        Student student4 = new Student("李观虾", 35.5, ".NET");
        Student student5 = new Student("林二", 89.4, "orcal");

        // 创建list,并把这些学生信息添加进去
        List<Student> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);

        // 用于存放学生列表,通过课程名,索引
        Map<String, Course> map = new HashMap<>(); 

        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            String course = stu.getCourseEve();
            double score = stu.getScore();

            Course value = map.get(course);

            if (null == value) {
                // 不存在, 添加到容器里
                //else里的操作交由Course类做了
                map.put(course, new Course(course, stu));
            } else {
                // 存在
                value.getStudents().add(stu);
                value.setTotal(value.getTotal() + score);
                //System.out.println("*****" + value.getTotal());
            }

        }

        Course course = map.get("Java");
        // 打印相同课程的平均分
        System.out.println(course.getTotal()+ "=======" + course.getStudents().size());
        System.out.println(course.getTitle() + "---平均分:--->" + course.getTotal()/course.getStudents().size());
        System.out.println("学生列表:");
        for (Student student6 : course.getStudents()) {
            System.out.println(student6.getCourseEve() + "********" + student6.getName() + "********" + student6.getScore());
        }

    }

}

结果如下:

164.4=======2**************[com.shsxtTest.Student@8b06bf9d, com.shsxtTest.Student@f0dd5981]
Java---平均分:--->82.2
学生列表:
Java********张还生********65.4
Java********徐凤年********99.0

心语

常用的容器:
    1、ArrayList: 集中统一处理多个对象 ,底层:数组
        增: add(E) add(int,E)
        遍历: 迭代器 、for+get(索引)  foreach
        删:remove(int) remove(E)
        改:set(int,E)
        查:size() get(int)
 2、HashMap: 从一堆中快速定位一个对象,底层:数组+链表
       增: put(k,v)
       遍历: keySet +get  entrySet+getKey|getValue 
       删:remove
       改: put(k,v)
       查: size  get(K)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值