import java.util.*;
class Student implements Comparable
{
private String id;
private String name;
Student(String id,String name)
{
this.id = id;
this.name = name;
}
public String getId()
{
return id;
}
public String getName()
{
return name;
}
public String toString()
{
return id +"::" + name;
}
public int hashCode()
{
return this.id.hashCode() + this.name.hashCode()*30;
}
public boolean equals(Object obj)
{
if(obj instanceof Student)
throw new ClassCastException("类型不符合");
Student s = (Student)obj;
return this.name.equals(s.name) && this.id.equals(s.id);
}
public int compareTo(Student s)
{
if(id.compareTo(s.id)==0)
return this.name.compareTo(s.name);
return this.id.compareTo(s.id);
}
}
class MyCmpt implements Comparator
{
public int compare(String o1,String o2)
{
return 1;
}
}
class MapDemo3
{
public static void demo()
{
TreeMap> czbk = new TreeMap>(new MyCmpt());
List<Student> yure = new ArrayList<Student>();
List<Student> jiuye = new ArrayList<Student>();
czbk.put("yure",yure);
czbk.put("jiuye",jiuye);
yure.add(new Student("01","zhangsan"));
yure.add(new Student("02","lisisan"));
jiuye.add(new Student("01","zhangsansan"));
jiuye.add(new Student("02","zhangxiaosan"));
Set<Map.Entry<String,List<Student>>> e = czbk.entrySet();
for(Iterator<Map.Entry<String,List<Student>>> it = e.iterator(); it.hasNext();)
{
Map.Entry<String,List<Student>> me = it.next();
String st = me.getKey();
List<Student> stu = me.getValue();
System.out.println(st);
getInfos(stu);
}
/*Set<String> s = czbk.keySet();
for(Iterator<String> it = s.iterator();it.hasNext();)
{
String str = it.next();
List<Student> stu = czbk.get(str);
System.out.println(str);
System.out.println(stu);
}*/
}
public static void getInfos(List<Student> list)
{
for(Iterator<Student> it = list.iterator();it.hasNext();)
{
Student s = it.next();
System.out.println(s);
}
}
public static void main(String[] args)
{
demo();
}
}
上面的代码我加入了比较器之后,用entrySet方法用迭代器去遍历键还是指向了值,但是我用keySet方法去遍历,键却指向了空,求解惑?