Question 10
Given: 
11. public class Person { 
12. private name; 
13. public Person(String name) { 
14. this.name = name; 
15. } 
16. public int hashCode() { 
17. return 420; 
18. } 
19. } 
Which is true? 
A. The time to find the value from HashMap with a Person key depends 
on the size of the map. 
B. Deleting a Person key from a HashMap will delete all map entries for 
all keys of type Person. 
C. Inserting a second Person object into a HashSet will cause the first 
Person object to be removed as a duplicate. 
D. The time to determine whether a Person object is contained in a 
HashSet is constant and does NOT depend on the size of the map. 
Answer: A 什么是HashMap?B 选项中的Person Key是什么意思,指的是什么??C选项中的HashSet又是什么?路过的神仙哥哥姐姐,哪个能够详细地给讲一下每个选项的对错及对错原因?谢谢。。感激流涕

解决方案 »

  1.   

    根据答案猜的:只覆盖了hashcode(),没有覆盖equals(),所以每次新插入都会在解决hash冲突的下一个位置插入,所以C错,D错,A对,B的话估计在删除的时候用了equals去比较,所以不会把所有的都删除。
      

  2.   

    可以google it! 也可以看任何一本关于java的书的 介绍容器的这一章节。
      

  3.   

    HashMap 和 HashSet都是集合中的内容,规则集(set),线性表(list),图(map)。
      HashSet是散列集,其中可以用来存放不同的任何元素,记住,一定是不同的。
      HashMap是一个散列图,key是键值。
      

  4.   

    HashMap 和 HashSet都是集合中的内容,规则集(set),线性表(list),图(map)。 
      HashSet是散列集,其中可以用来存放不同的任何元素,记住,一定是不同的,而且没有顺序。 
      HashMap是一个散列图,key是键值,它是一种key - vaule的形式存储的。
      

  5.   

    有关的内容在集合,还有hashCode(),集合里存放的是一个对象一个值即键值对,hashCode()就是生成相应对象值的
      

  6.   

    按自己的理解翻译下这四个选项,错误的地方还请指正HasnMap和HashSet简单点讲,HashSet中的数据不允许重复A. The time to find the value from HashMap with a Person key depends
    on the size of the map.//按Person key检索该HashMap所花费的时间取决于该HashMap的大小。无疑正确的B. Deleting a Person key from a HashMap will delete all map entries for
    all keys of type Person.//从该HashMap中删除一个Person key时,会导致已有的纪录被删除。很明显错误C. Inserting a second Person object into a HashSet will cause the first
    Person object to be removed as a duplicate.//插入新纪录到该HashSet中时会导致前一条因重复被移除。不一定,如果此前有相同记录会被覆盖掉,否则不会,所以此说法也不对D. The time to determine whether a Person object is contained in a
    HashSet is constant and does NOT depend on the size of the map.//A的反面说法,所以错误
      

  7.   

    C:选项hashSet是不允许有重复的,C是正确的。
      

  8.   

    map中存储的是键值对,personkey的意思是,你以Person对象为键来存储。
      

  9.   

    C是不正确的,我试过了。我疑惑的是set不允许重复是指的什么,对于同一个对象,
    不能插入两次,负责,第二个会覆盖第一个,对于两个对象,虽然你的hashCode一样,
    还是允许插入。
      

  10.   

    明白了,因为这个person没有写equals函数,写了之后,就不会允许插入相同的对象(不管是同一个对象的两次
    插入,还是两个相同的对象)
      

  11.   

    看了很多次这贴子,发觉没有确切的答案,网上也没有相关的解析。所以特发贴,希望对不明白的人有所帮助。我写了完整的代码。先看代码。public class Person {
        private String name;
     public Person(String name) {
        this.name = name;
     }
     public int hashCode() {
        return 420;
     }
    /*这里我重写了equals方法,先不用细看。
     public boolean equals(Object obj){
         Person p=(Person)obj;
         return getName().equals(p.getName());
     }
    */
     public String getName(){
         return this.name;
     }
     public static void main(String args[]){
        Person p1=new Person("张三");
        Person p2=new Person("张三");
        Person p3=p1;
        Person p4=new Person("李四");    HashSet<Person> set=new HashSet<Person>();    set.add(p1);
        set.add(p2);
        set.add(p3);
        set.add(p4);    for(Person i:set){
            System.out.print(i.getName()+",");
        }
    }
    }
    =======================================================
    运行结果:
    李四,张三,张三,成功生成(总时间:3 秒)这是没有重写equals方法的结果,可以看出,可以插入对象,即使对象名字是相同的,这就是没有重些equals的缘故,而相同的引用没法插入。p3没有插入成功。
    答案C,解析是插入一个对象会覆盖第一个对象,如上解析得出C是错误的。讨论:重写了equals方法的结果:
    李四,张三,成功生成(总时间:4 秒)这里不用我解析了吧。总结:只重写hashCode方法没法导致插入到HashSet的东西发生覆盖,即使hash码是相同的也不能覆盖,而相同的引用是不能插入的,以上是本人愚见。