package jihe01;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MapDemo4 {
public static void main(String args[]){
method();
}
public static void method(){
HashMap<String ,List<student2>> csdn=new HashMap<String, List<student2>>();
List<student2> yure=new ArrayList<student2>();
List<student2> jiuye=new ArrayList<student2>();
csdn.put("yure",yure);
csdn.put("jiuye",jiuye);
yure.add(new student2("01","zhangsan"));
yure.add(new student2("02","lisi"));
yure.add(new student2("01","zhangsan"));
jiuye.add(new student2("01","wangwu"));
jiuye.add(new student2("02","zhaoliu"));
Iterator<String > it=csdn.keySet().iterator();
while(it.hasNext()){
String roomName=it.next();
List<student2> room=csdn.get(roomName);
sop(roomName);
getStu(room);
}
}
public static void getStu(List<student2> room){
Iterator<student2> it=room.iterator();
while(it.hasNext()){
sop(it.next());
}
}
public static void sop(Object obj){
System.out.println(obj);
}
}
class student2 implements Comparable<student2>{
private String id;
private String name;
public student2(String id,String name){
this.id=id;
this.name=name;
}
public String toString(){
return id+"..."+name;
}
public int compareTo(student2 student){
int num=new Integer(id.hashCode()).compareTo(new Integer(student.id.hashCode()));
if(num==0)
return name.compareTo(student.name);
return num;
}
public int hashCode(){
return name.hashCode()+id.hashCode();
}
public boolean equals(Object obj){
if(!(obj instanceof student2))
throw new ClassCastException("类型不匹配");
student2 student=(student2)obj;
return name.equals(student.name) && id.equals(student.id);
}
}
为什么还能将重复元素添加进来
|
|