使用工具
IDEA2018.2
使用说明
- 使用迭代器,即创建Iterator对象,Iterator是一个接口,也有泛型
- hasNext方法判断是否已经遍历完,
- next方法取当前遍历的对象
- HashMap用keySet()方法获得键的Set集合
- values()方法获得值的Set集合
- entrySet()方法获得键值对的Set集合
代码示例
Student.java
package com.vvcat;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/10/13 19:37
* @Version 1.0
**/
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age;
public Student() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(Integer id, String name) {
this.id = id;
this.name = name;
}
public Student(Integer id, String name, String sex, Integer age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
'}';
}
}
HashMap .java
package com.vvcat;
import java.util.*;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/10/13 19:39
* @Version 1.0
**/
public class HashMap {
public static void main(String[] args) {
//实例化3个学生
Student stu1 = new Student(1, "张三","男",20);
Student stu2 = new Student(2, "李四","男",21);
Student stu3 = new Student(3, "赵五","男",22);
// 实例化HashMap用于保存学生
java.util.HashMap<Integer, Student> map = new java.util.HashMap<Integer, Student>();
//建立学号与学生的映射关系
map.put(stu1.getId(),stu1);
map.put(stu2.getId(),stu2);
map.put(stu3.getId(),stu3);
// 遍历方法1.通过Map.keySet遍历key和value(经常使用,二次取值)
Set<Integer> keys = map.keySet();
Iterator<Integer> it1 = keys.iterator();
System.out.println("遍历方法1,通过keySet遍历key和value获得键集合 ");
for (Integer key : map.keySet()) {
System.out.println("key = "+ key + " and value= " + map.get(key));
}
while (it1.hasNext()){
Student st = map.get(it1.next());
System.out.println("学号:" + st.getId() + "\t" + "姓名:" + st.getName() + "\t" + "性别:" + st.getSex() + "\t" + "年龄:" + st.getAge());
}
System.out.println("---------------------------");
// 遍历方法2.通过Map.values()遍历所有的value,但不能遍历key
Collection<Student> values = map.values();
Iterator<Student> it2 = values.iterator();
System.out.println("遍历方法2,通过values获得值集合");
for (Student stu : map.values()) {
System.out.println("value= " + stu);
}
while(it2.hasNext()){
Student next = it2.next();
System.out.println("学号:" + next.getId() + "\t"+ "姓名:" + next.getName()+ "\t" + "性别:" + next.getSex() + "\t"+ "年龄:" + next.getAge());
}
System.out.println("---------------------------");
// 遍历方法3.通过Map.entrySet使用iterator遍历key和value:
Set<Map.Entry<Integer, Student>> kys = map.entrySet();
Iterator<Map.Entry<Integer, Student>> it3 = kys.iterator();
System.out.println("遍历方法3,通过entrySet使用iterator遍历key和value获得键值对");
while(it3.hasNext()){
Student st = it3.next().getValue();
System.out.println("学号:" + st.getId() + "\t" + "姓名:" + st.getName()+ "\t" + "性别:" + st.getSex()+ "\t" + "年龄:" + st.getAge());
}
System.out.println("---------------------------");
//遍历方法4:通过Map.entrySet遍历key和value(推荐容量大时使用)
System.out.println("通过Map.entrySet遍历key和value");
for (Map.Entry<Integer, Student> entry : map.entrySet()) {
Student st = entry.getValue();
System.out.println("学号:" + entry.getKey()+ "\t" + "姓名:" + st.getName()+ "\t" + "性别:" + st.getSex()+ "\t" + "年龄:" + st.getAge());
}
}
}