Student类。
package week4HomeWork;
public class Student {
private String name;
private int num;
private String address;
public String toString() {
return "Student [name=" + name + ", num=" + num + ", address="
+ address + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student() {
super();
}
public Student(String name, int num, String address) {
super();
this.name = name;
this.num = num;
this.address = address;
}
}
下面是测试类
package week4HomeWork;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class TestMap {
public static void main(String[] args) {
Student s1= new Student("张三",12,"北京");
Student s2= new Student("李四",14,"北京");
Student s3= new Student("王五",13,"北京");
Map<String,Student> map1= new HashMap<String,Student>();
// String a = new Scanner(System.in).next();
String a1= "张三";
map1.put(s1.getName(), s1);
map1.put(s2.getName(), s2);
map1.put(s3.getName(), s3);
Set<Entry<String, Student>> m=map1.entrySet();
Iterator<Entry<String, Student>> iter =m.iterator() ;
while(iter.hasNext()){
Entry<String,Student> e=iter.next();
System.out.println("这是key值--"+e.getKey()+"--"+"学生对象--"+e.getValue());
}
map1.remove(a1);
System.out.println("******************************");
Set<Entry<String, Student>> m1=map1.entrySet();
Iterator<Entry<String, Student>> iter1 =m.iterator() ;
while(iter1.hasNext()){
Entry<String,Student> e=iter1.next();
System.out.println("这是key值--"+e.getKey()+"--"+"学生对象--"+e.getValue());
}
}
}
运行结果