新手上路之List,HashMap遍历二(1对多) 笔记总结
来源:互联网 发布:php一般用什么开发工具 编辑:程序博客网 时间:2024/06/06 17:02
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.4Java********徐凤年********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
- 新手上路之List,HashMap遍历二(1对多) 笔记总结
- 新手上路之容器HashMap遍历总结 笔记
- 新手上路之容器ArrayList遍历总结 笔记
- 新手上路之监听器 笔记
- 新手上路之面向对象 笔记
- 新手上路之oracle 事务 笔记
- 新手上路之oracle JDBC 笔记
- 新手上路之AJAX AJAX笔记小结
- 新手上路之c指针理解 笔记
- PHP新手上路(二)
- driver之新手上路
- 新手上路 jquery 选择器篇(二) 代码笔记
- PHP新手上路(二)
- PHP新手上路(二)
- 新手上路之oracle 92 99 表连接(笔记)
- 新手上路之oracle 视图 索引(了解)笔记
- 新手上路之oracle 增,删,改,查操作 笔记
- 新手上路之 js 创建对象篇 代码笔记
- Spring事务管理总结
- MD5加密算法
- DOS中del和rd的区别
- 机器学习笔记1绪论----教材周志华西瓜书
- 如何分辨设备并判断机型???
- 新手上路之List,HashMap遍历二(1对多) 笔记总结
- Java 数组拷贝
- 动态顺序表C++实现
- 2018小红书笔试题第一题
- 07:矩阵归零消减序列和
- 如何写好科研论文的摘要
- 洛谷 P2002 消息扩散
- CSDN转载博客的方法
- luogu1202 [USACO1.1]黑色星期五Friday the Thirteenth