您的位置:首页 > 其它

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

2017-09-28 20:29 417 查看
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) {
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