hashcode equals Float.floatToIntBits()

来源:互联网 发布:药物设计软件 编辑:程序博客网 时间:2024/05/16 13:45
先看下方法介绍
按照IEEE   754标准,32位浮点数在计算机中二进制存储形式共三部分:S(1位,符号)   E(8位,阶码)   M(23位,尾数)
给你举个例子吧,Float.floatToIntBits(20.5f)按照如下方式计算:
20.59D=10100.1B=1.01001*2^4B   指数e=4
S=0--> 正数     E=4+127=131D=10000011B--> 真实指数e变成阶码E时需加127     M=01001B
则32位2进制存储形式为:0   10000011   01001000000000000000000
转换成10进制即1101266944
1.何时需要重写equals()
当一个类有自己特有的“逻辑相等”概念(不同于对象身份的概念)。
2.设计equals()
[1]使用instanceof操作符检查“实参是否为正确的类型”。
[2]对于类中的每一个“关键域”,检查实参中的域与当前对象中对应的域值。
[2.1]对于非floatdouble类型的原语类型域,使用==比较;
[2.2]对于对象引用域,递归调用equals方法;
[2.3]对于float域,使用Float.floatToIntBits(afloat)转换为int,再使用==比较;
[2.4]对于double域,使用Double.doubleToLongBits(adouble) 转换为int,再使用==比较;
[2.5]对于数组域,调用Arrays.equals方法。
3.当改写equals()的时候,总是要改写hashCode()
根据一个类的equals方法(改写后),两个截然不同的实例有可能在逻辑上是相等的,但是,根据Object.hashCode方法,它们仅仅是两个对象。因此,违反了“相等的对象必须具有相等的散列码”。
4.设计hashCode()
[1]把某个非零常数值,例如17,保存在int变量result中;
[2]对于对象中每一个关键域f(指equals方法中考虑的每一个域):
[2.1]boolean型,计算(f ? 0 : 1);
[2.2]byte,char,short型,计算(int);
[2.3]long型,计算(int) (f ^ (f>>>32));
[2.4]float型,计算Float.floatToIntBits(afloat);
[2.5]double型,计算Double.doubleToLongBits(adouble)得到一个long,再执行[2.3];
[2.6]对象引用,递归调用它的hashCode方法;
[2.7]数组域,对其中每个元素调用它的hashCode方法。
[3]将上面计算得到的散列码保存到int变量c,然后执行 result=37*result+c;
[4]返回result
5.示例
下面的这个类遵循上面的设计原则,重写了类的equals()hashCode()
Java代码 复制代码
  1. package com.zj.unit;   
  2. import java.util.Arrays;   
  3.     
  4. public class Unit {   
  5.     private short ashort;   
  6.     private char achar;   
  7.     private byte abyte;   
  8.     private boolean abool;   
  9.     private long along;   
  10.     private float afloat;   
  11.     private double adouble;   
  12.     private Unit aObject;   
  13.     private int[] ints;   
  14.     private Unit[] units;   
  15.     
  16.     public boolean equals(Object o) {   
  17.        if (!(o instanceof Unit))   
  18.            return false;   
  19.        Unit unit = (Unit) o;   
  20.        return unit.ashort == ashort   
  21.               && unit.achar == achar   
  22.               && unit.abyte == abyte   
  23.               && unit.abool == abool   
  24.               && unit.along == along   
  25.               && Float.floatToIntBits(unit.afloat) == Float   
  26.                      .floatToIntBits(afloat)   
  27.               && Double.doubleToLongBits(unit.adouble) == Double   
  28.                      .doubleToLongBits(adouble)   
  29.               && unit.aObject.equals(aObject)    
  30. && equalsInts(unit.ints)   
  31.               && equalsUnits(unit.units);   
  32.     }   
  33.     
  34.     private boolean equalsInts(int[] aints) {   
  35.        return Arrays.equals(ints, aints);   
  36.     }   
  37.     
  38.     private boolean equalsUnits(Unit[] aUnits) {   
  39.        return Arrays.equals(units, aUnits);   
  40.     }   
  41.     
  42.     public int hashCode() {   
  43.        int result = 17;   
  44.        result = 37 * result + (int) ashort;   
  45.        result = 37 * result + (int) achar;   
  46.        result = 37 * result + (int) abyte;   
  47.        result = 37 * result + (abool ? 0 : 1);   
  48.        result = 37 * result + (int) (along ^ (along >>> 32));   
  49.        result = 37 * result + Float.floatToIntBits(afloat);   
  50.        long tolong = Double.doubleToLongBits(adouble);   
  51.        result = 37 * result + (int) (tolong ^ (tolong >>> 32));   
  52.        result = 37 * result + aObject.hashCode();   
  53.        result = 37 * result + intsHashCode(ints);   
  54.        result = 37 * result + unitsHashCode(units);   
  55.        return result;   
  56.     }   
  57.     
  58.     private int intsHashCode(int[] aints) {   
  59.        int result = 17;   
  60.        for (int i = 0; i < aints.length; i++)   
  61.            result = 37 * result + aints[i];   
  62.        return result;   
  63.     }   
  64.     
  65.     private int unitsHashCode(Unit[] aUnits) {   
  66.        int result = 17;   
  67.        for (int i = 0; i < aUnits.length; i++)   
  68.            result = 37 * result + aUnits[i].hashCode();   
  69.        return result;   
  70.     }   
  71. }   
  72.     
 
6.看看eclipse自动生成的equals和hashcode方法,还是很好的
这个是我自己补充:
Java代码 复制代码
  1. public class A {   
  2.  String a;   
  3.     int b=0;   
  4.     String[]c;   
  5.     float d;   
  6.     A a1;   
  7.     A[] a2;   
  8.  @Override  
  9.  public int hashCode() {   
  10.   final int prime = 31;   
  11.   int result = 1;   
  12.   result = prime * result + ((a == null) ? 0 : a.hashCode());   
  13.   result = prime * result + ((a1 == null) ? 0 : a1.hashCode());   
  14.   result = prime * result + Arrays.hashCode(a2);   
  15.   result = prime * result + b;   
  16.   result = prime * result + Arrays.hashCode(c);   
  17.   result = prime * result + Float.floatToIntBits(d);   
  18.   return result;   
  19.  }   
  20.  @Override  
  21.  public boolean equals(Object obj) {   
  22.   if (this == obj)   
  23.    return true;   
  24.   if (obj == null)   
  25.    return false;   
  26.   if (getClass() != obj.getClass())   
  27.    return false;   
  28.   A other = (A) obj;   
  29.   if (a == null) {   
  30.    if (other.a != null)   
  31.     return false;   
  32.   } else if (!a.equals(other.a))   
  33.    return false;   
  34.   if (a1 == null) {   
  35.    if (other.a1 != null)   
  36.     return false;   
  37.   } else if (!a1.equals(other.a1))   
  38.    return false;   
  39.   if (!Arrays.equals(a2, other.a2))   
  40.    return false;   
  41.   if (b != other.b)   
  42.    return false;   
  43.   if (!Arrays.equals(c, other.c))   
  44.    return false;   
  45.   if (Float.floatToIntBits(d) != Float.floatToIntBits(other.d))   
  46.    return false;   
  47.   return true;   
  48.  }   
  49.     
  50. }  
 
这里可以看到它没有能自动实现一个类里面有属于自己类型的属性的hashcode,结合上面标题5的思想设计,就比较完美了,但这个equals()方法的头几步判断是比标题5里面的好的。