自己编写HashCodeBuilder类

jakarta-commons-lang已经实现了HashCodeBuilder类,据闻在Hibernate中出错,所以还是选择了自己实现,编码思想来源于 Effective java 一书。而且0依赖。

java 代码
 
  1. import java.lang.reflect.Array;  
  2.   
  3. public class HashCodeBuilder {  
  4.     public HashCodeBuilder() {  
  5.   
  6.     }  
  7.   
  8.     private int result = 17;  
  9.   
  10.     public HashCodeBuilder append(boolean field) {  
  11.         result = 37 * result + (field ? 1 : 0);  
  12.         return this;  
  13.     }  
  14.   
  15.     public HashCodeBuilder append(byte field) {  
  16.         result = 37 * result + (int) field;  
  17.         return this;  
  18.     }  
  19.   
  20.     public HashCodeBuilder append(char field) {  
  21.         result = 37 * result + (int) field;  
  22.         return this;  
  23.     }  
  24.   
  25.     public HashCodeBuilder append(short field) {  
  26.         result = 37 * result + (int) field;  
  27.         return this;  
  28.     }  
  29.   
  30.     public HashCodeBuilder append(int field) {  
  31.         result = 37 * result + field;  
  32.         return this;  
  33.     }  
  34.   
  35.     public HashCodeBuilder append(long field) {  
  36.         result = 37 * result + (int) (field ^ (field >>> 32));  
  37.         return this;  
  38.     }  
  39.   
  40.     public HashCodeBuilder append(float field) {  
  41.         result = 37 * result + Float.floatToIntBits(field);  
  42.         return this;  
  43.     }  
  44.   
  45.     public HashCodeBuilder append(double field) {  
  46.         append(Double.doubleToLongBits(field));  
  47.         return this;  
  48.     }  
  49.   
  50.     public HashCodeBuilder append(Object field) {  
  51.         if(field == null)  
  52.             result = 0;  
  53.         else if (field.getClass().isArray()) {  
  54.             for (int i = Array.getLength(field) - 1; i >= 0; i--) {  
  55.                 append(Array.get(field, i));  
  56.             }  
  57.         } else  
  58.             append(field.hashCode());  
  59.         return this;  
  60.     }  
  61.   
  62.     public int toHashCode() {  
  63.         return result;  
  64.     }  
  65.       
  66.     @Override  
  67.     public int hashCode() {  
  68.         return result;  
  69.     }  
  70.       
  71.     @Override  
  72.     public String toString() {  
  73.         return String.valueOf(result);  
  74.     }  
  75.   
  76. }  



测试代码

java 代码
 
  1. public class MyBean {  
  2.     Long id;  
  3.     String name;  
  4.     char[] chars;  
  5.     public MyBean(Long id, String name, char[] chars) {  
  6.         super();  
  7.         this.id = id;  
  8.         this.name = name;  
  9.         this.chars = chars;  
  10.     }  
  11.       
  12.     @Override  
  13.     public int hashCode() {  
  14.         return new HashCodeBuilder().append(id)  
  15.                     .append(name).append(chars).hashCode();  
  16.     }  
  17.       
  18.     public static void main(String[] args) {  
  19.         MyBean bean1 = new MyBean(Long.valueOf(10),  
  20.                 "名称",new char[]{'a','b','c','d','e'});  
  21.         MyBean bean2 = new MyBean(Long.valueOf(10),  
  22.                 "名称",new char[]{'a','b','c','d','e'});  
  23.         System.out.println("bean1.hashCode="+bean1.hashCode());  
  24.         System.out.println("bean2.hashCode="+bean2.hashCode());  
  25.     }  
  26. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值