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又是什么?路过的神仙哥哥姐姐,哪个能够详细地给讲一下每个选项的对错及对错原因?谢谢。。感激流涕
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又是什么?路过的神仙哥哥姐姐,哪个能够详细地给讲一下每个选项的对错及对错原因?谢谢。。感激流涕
解决方案 »
- 线程 sleep 问题
- 多线程信息传递
- DBView,DBTable怎么在JDK1。42里找不到啊?
- JTree的TreeModel、TreeNode,TreePath之间的联系啊 请大哥们指点
- 如何将一个jar(如comm.jar)加到我的JBuilder2005工程中去,这样我就可以写下如下代码import javax.comm.* 而不出错???
- 能不能改变java控制台窗口的字体颜色?
- 为什么用delphi和java针对同一信息做DES算法加密出来的结果不一样?
- 初学java,搞不清这些问题
- 可怜我吧!就这么多分了!!各位!在tomcat 4.0.1和tomcat 4.0.2中怎么样建立一个新的虚拟目录????
- 请java高手回答
- lucene+heritrix day1
- java 修改数据库内容的语句
HashSet是散列集,其中可以用来存放不同的任何元素,记住,一定是不同的。
HashMap是一个散列图,key是键值。
HashSet是散列集,其中可以用来存放不同的任何元素,记住,一定是不同的,而且没有顺序。
HashMap是一个散列图,key是键值,它是一种key - vaule的形式存储的。
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的反面说法,所以错误
不能插入两次,负责,第二个会覆盖第一个,对于两个对象,虽然你的hashCode一样,
还是允许插入。
插入,还是两个相同的对象)
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码是相同的也不能覆盖,而相同的引用是不能插入的,以上是本人愚见。