import java.util.*;
import java.util.Map.Entry;
public class TreeMapDemo
{
public static void main(String args[])
{
TreeMap<Student,String> tm=new TreeMap<Student,String>();
tm.put(new Student("lisi",20),"beijing");
tm.put(new Student("zhangsan",19),"nanjing");
Set<Map.Entry<Student,String>> entryset=tm.entrySet();
Iterator<Map.Entry<Student,String>> it=entryset.iterator();
while(it.hasNext())
{
Map.Entry<Student,String> ms=it.next();
Student s= tm.getKey();
String add=tm.getValue();
System.out.println(s.getName()+"::"+s.getAge());
}
}
/*public static void main(String args[])
{
TreeMap<Student,String> tm=new TreeMap<Student,String>();
tm.put(new Student("lisi",20),"beijing");
tm.put(new Student("zhangsan",19),"nanjing");
tm.put(new Student("wangwu",20),"jiangxi");
Set<Student> keyset=tm.keySet();
Iterator<Student> it=keyset.iterator();
while(it.hasNext())
{
Student s=it.next();
System.out.println(s.getName()+"::"+s.getAge());
}
}
*/
}
class Student implements Comparable<Student>
{
private String name;
private int age;
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public int compareTo(Student s)
{
int num=new Integer(this.age).compareTo(new Integer(s.age));
if(num==0)
return new Integer(this.name.length()).compareTo(new Integer(s.name.length()));//名字按字符串长度来判断
return num;
}
}
发生了这两个错误The method getKey() is undefined for the type TreeMap<Student,String>
The method getValue() is undefined for the type TreeMap<Student,String>
|