vfdsgv
idea吧
全部回复
仅看楼主
level 1
import java.util.*;
public class HashMapDemo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("001",new Student("Lucy",20)); //(1)向HashMap中添加键值对
map.put("002",new Student("Hellen",19));
map.put("003",new Student("Andrew",21));
map.put("001",new Student("Jimmy",19)); //键重复,覆盖原键值对
// Student s1 = (Student)map.get("003"); //(2)按键读取数据
// System.out.println(s1.getName()+","+s1.getAge()); //输出Jimmy,19
//
Set keys = map.keySet(); //(1)取出map中的键集
Iterator it = keys.iterator();
while(it.hasNext()){
String key = (String)it.next();
Student stu = (Student)map.get(key); //(2)按键从map中获取对应的值
System.out.println(key+":"+"("+stu.getName()+","+stu.getAge()+")");
}
}
}
////////
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2021年05月15日 08点05分 1
level 13

2021年05月24日 08点05分 2
1