余生不过两万天1992 2016-01-14 14:32 采纳率: 0%
浏览 1426

KeySet跟entrySet为什么会有两种不同的结果。。

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方法去遍历,键却指向了空,求解惑?

  • 写回答

3条回答 默认 最新

  • danielinbiti 2016-01-14 15:27
    关注

    问题在于

     czbk = new TreeMap>(new MyCmpt());//这里设置了比较器
     但你的MyCmpt定义只有一种返回值
     class MyCmpt implements Comparator
    {
    public int compare(String o1,String o2)
    {
    return 1;
    }
    也就是不管什么key值,都取右节点接着找,直到末尾,最终因为找不到,所以返回null。
    这里改成key值相等的时候就返回
    public int compare(Object o1,Object o2)
    {
    return ((String)o1).equals(o2)?0:1;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog