zhb8015

posts(23) comments(6) trackbacks(0)
  • BlogJava
  • 联系
  • RSS 2.0 Feed 聚合
  • 管理

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论

留言簿

  • 给我留言
  • 查看公开留言
  • 查看私人留言

随笔分类

  • hadoop

随笔档案

  • 2013年3月 (1)
  • 2012年10月 (2)
  • 2012年8月 (2)
  • 2012年7月 (1)
  • 2012年6月 (1)
  • 2012年5月 (1)
  • 2012年4月 (5)

文章分类

  • arithmetc
  • books(2)
  • design patter(4)
  • English(1)
  • exception(3)
  • hadoop(1)
  • interview(53)
  • Kent Beck
  • linux,unix(1)
  • MartinFlow(7)
  • method(7)
  • middleware(1)
  • projectManagement(6)
  • soa(9)
  • ssh(14)
  • ThoughtWork(2)
  • tibco(13)

文章档案

  • 2013年4月 (1)
  • 2013年3月 (3)
  • 2012年8月 (1)
  • 2012年7月 (8)
  • 2012年6月 (15)
  • 2012年5月 (14)
  • 2012年4月 (22)
  • 2012年3月 (5)

相册

  • java

搜索

  •  

最新评论

  • 1. re: Log4j详细配置(转)
  • 写得很详细,最后那句好像有点小问题,输出到test1和stdout应该是log4j.logger.myTest1=DEBUG, test1, stdout ?
  • --aramxiao
  • 2. re: 结合Maven2进行J2EE项目构建(转)
  • 评论内容较长,点击标题查看
  • --最代码
  • 3. re: java深浅复制
  • 评论内容较长,点击标题查看
  • --zhb8015
  • 4. re: 求质数,难以理解的代码,有兴趣可以看一下
  • 评论内容较长,点击标题查看
  • --zhb8015
  • 5. re: Advice about migrating to new platfrom
  • platfrom or platform??
  • --qingyue

阅读排行榜

评论排行榜

View Post

hashcode面试题

Java code
public class ValuePair { public int a = 4,b; public boolean equals(Object other){ try{ ValuePair o = (ValuePair) other; return (a==o.a&&b==o.b)||(a==o.b&&b==o.a); }catch(ClassCastException cce){ return false; } } public int hashCode(){ //请选择下边的答案(多选) } }

A。return 0;
B. return a;
C. return a+b;
D. return a-b;
E. return a^b;
F. return (a<<16)|b;
请说明理由。


根据一般约定,如果2个对象 equals 比较为true,那么hashCode也最好(不是必须)相同。
  如果2个对象 equals 比如为false,那么hashCode也最好(不是必须)不同


因此对于这个题目,如果从纯语法层面考虑,全部都是可选的
但是从程序运行效率来看,最好是该hashCode里,a,b值可以互换,而又尽可能做到不同的a,b值返回不同的值。因为C,E最好.
  
A。return 0; 效率太低
B. return a; 不满足a,b的互换性
D. return a-b; 不满足a,b的互换性
F. return (a < <16)|b; 不满足a,b的互换性

另外,比如 a * b 也满足互换性,但是效率稍低,毕竟没加法和位运算快


* Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. 
  * If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 
  * It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. 

posted on 2012-05-30 16:08 zhb8015 阅读(437) 评论(0)  编辑  收藏 所属分类: interview

新用户注册  刷新评论列表  

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问   管理
相关文章:
  • Nutch vs Lucene(转)
  • ArrayList空间增长是怎么样的
  • 手工打包(jar)
  • TW interview experience(转)
  • 排序算法(转)
  • 加密算法(转)
  • 软件设计过程一些术语 AN BD FD DD CD CT
  • Log4j详细配置(转)
  • log4j详解
  • 时间复杂度
 
 
Powered by:
BlogJava
Copyright © zhb8015