import java.util.*;
class StuNameComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
int num = s1.getName().compareTo(s2.getName());
if(num==0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
return num;
}
}
class MapTest2
{
public static void main(String[] args)
{
TreeMap<Student,String> hm = new TreeMap<Student,String>(new StuNameComparator());
hm.put(new Student("lishi01",24),"beijing");
hm.put(new Student("lishi03",25),"wuahni");
hm.put(new Student("lishi05",23),"shanghai");
hm.put(new Student("lishi02",44),"nanjing");
Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Student,String> me = it.next();
Student stu = me.getKey();
String adrr = me.getValue();
System.out.println(stu+"...."+adrr);
}
}
}
---------- javac ----------
MapTest2.java:11: 错误: 找不到符号
int num = s1.getName().compareTo(s2.getName());
^
符号: 方法 getName()
位置: 类型为Student的变量 s2
MapTest2.java:11: 错误: 找不到符号
int num = s1.getName().compareTo(s2.getName());
^
符号: 方法 getName()
位置: 类型为Student的变量 s1
MapTest2.java:13: 错误: 找不到符号
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
^
符号: 方法 getAge()
位置: 类型为Student的变量 s2
MapTest2.java:13: 错误: 找不到符号
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
^
符号: 方法 getAge()
位置: 类型为Student的变量 s1
4 个错误