新手上路之List,HashMap遍历二(1对多) 笔记总结
2017-09-28 20:29
417 查看
Student 类,用于单个学生的信息(1):
学生列表(set容器添加),Course类(N):
实例代码TestStudentList 类:
结果如下:
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) { th d115 is.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)
相关文章推荐
- 新手上路之容器HashMap遍历总结 笔记
- 新手上路之容器ArrayList遍历总结 笔记
- 【新手学习笔记】我总结的unity遍历所有子物体的两种简单方法
- HashMap和List遍历方法总结及如何遍历删除元素
- 反向遍历list的思路及js及json的总结
- 新手上路之oracle JDBC 笔记
- selenium 学习笔记 ---新手学习记录(10) 问题总结(java)--poi--excel 操作
- [Java] 遍历HashMap和HashMap转换成List的两种方式
- 软件项目经理新手上路(12) - 案例总结,项目经理的演变
- 第16章 List集合的总结和遍历
- 【转】周末班LR笔记总结—新手入门必备
- PHP循环遍历数组的3种方法list()、each()和while总结
- 软件项目经理新手上路12 - 案例总结,项目经理的演变 推荐
- 新手上路之oracle 视图 索引(了解)笔记
- Scala List操作笔记 --备忘(9.9总结)
- QStringList类常用方法总结|QStringList 遍历
- 新手上路之监听器 笔记
- C++总结笔记(九)常见容器--List
- maven2学习 新手上路(笔记形式)
- 新手菜鸟学习C#的笔记总结 之数组与集合(下)