本帖最后由 黄兴旺 于 2013-8-25 16:42 编辑
public class MapTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//提示此处存在问题?HashMap类找不到
Map<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("zhangsan",11),"北京");
hm.put(new Student("lisi",12),"天津");
hm.put(new Student("lisi",12),"武汉");
hm.put(new Student("wangwu",13),"上海");
hm.put(new Student("zhaoliu",14),"重庆");
//System.out.println(hm.get("北京"));
Set<Map.Entry<Student,String>> set=hm.entrySet();
Iterator<Map.Entry<Student,String>> it=set.iterator();
while(it.hasNext()){
Map.Entry<Student,String> me=it.next();
Student s1=me.getKey();
String s2=me.getValue();
//System.out.println(s1);
System.out.println(s1+"==="+s2.toString());
}
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String toString(){
return name+"||"+age;
}
public int hashCode(){
return 33;
}
public boolean equals(Student s){
return s.getName()==s.getName()&&s.getAge()==s.getAge();
}
public int compareTo(Student s){
int num=new Integer(this.age).compareTo(new Integer(s.age));
if(num==0)
return this.name.compareTo(s.name);
return num;
}
}
|