zoukankan      html  css  js  c++  java
  • hash code

    值相同却可能有不同的hashcode

      //对象值到底指什么?(x.equals(y) == true)应该并不代表对象值相同
    class  A
    {
    A(){}
    public boolean equals(A a)
    {
    return true;
    }
    }
    public class EqualsTest
    {
    public static void main(String argv[])
    {
    A a1 = new A();
    A a2 = new A();
    System.out.println(a1.equals(a2));
    System.out.println(a1.hashCode());
    System.out.println(a2.hashCode());
    }
    }
    

      hashCode是jdk根据对象的地址或者字符串或者数字算出来的int类型的数值 详细了解请 参考 [1] public inthashCode()返回该对象的哈希码值。支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能。

    协定

    一致性

    在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行hashcode比较时所用的信息没有被修改。

    equals

    如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果,注:这里说的equals(Object) 方法是指Object类中未被子类重写过的equals方法。
    如果两个hashCode()返回的结果相等,则两个对象的equals方法不一定相等。

    附加

    如果根据equals(java.lang.Object)方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。

    重写

    HashMap对象是根据其Key的hashCode来获取对应的Value。
    在重写父类的equals方法时,也重写hashcode方法,使相等的两个对象获取的HashCode也相等,这样当此对象做Map类中的Key时,两个equals为true的对象其获取的value都是同一个,比较符合实际。
    public class Person {
    	int age;
    	
    	@Override
    	public boolean equals(Object obj) {
    		//按照你想要的方法去比较,比如我这里比较的是年龄,年龄相等就返回true
    		if(!(obj instanceof Person))
    			return false;
    		Person p = (Person)obj;
    		return this.age==p.age?true:false;
    	}
    
    	public static void main(String[] args) {
    		Person p1 = new Person();
    		Person p2 = new Person();
    		p1.age=1;
    		p2.age=1;
    		System.out.println(p1.equals(p2));//如果没有重写equals方法,Object默认是比较他们的引用,所以返回的是false,你可以试试
    		
    	}
    }
    

      

    equals()方法的重写

    一、为什么equals()方法要重写?

    判断两个对象在逻辑上是否相等,如根据类的成员变量来判断两个类的实例是否相等,而继承Object中的equals方法只能判断两个引用变量是否是同一个对象。这样我们往往需要重写equals()方法。

    我们向一个没有重复对象的集合中添加元素时,集合中存放的往往是对象,我们需要先判断集合中是否存在已知对象,这样就必须重写equals方法。

    二、怎样重写equals()方法?

    • 重写equals方法的要求:
      1.自反性:对于任何非空引用x,x.equals(x)应该返回true。
      2.对称性:对于任何引用x和y,如果x.equals(y)返回true,那么y.equals(x)也应该返回true。
      3.传递性:对于任何引用x、y和z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)也应该返回true。
      4.一致性:如果x和y引用的对象没有发生变化,那么反复调用x.equals(y)应该返回同样的结果。
      5.非空性:对于任意非空引用x,x.equals(null)应该返回false。

    格式:

    Java代码  收藏代码
    1. public boolean equals(Object obj) {  
    2.     if(this == obj)  
    3.         return false;  
    4.     if(obj == null)  
    5.         return false;  
    6.     if(getClass() != obj.getClass() )  
    7.         return false;  
    8.     MyClass other = (MyClass)obj;  
    9.     if(str1 == null) {  
    10.          if(obj.str1 != null) {  
    11.               return false;  
    12.          }  
    13.     }else if (!str1.equals(other.str1) )  
    14.              return false;  
    15.      }  
    16.     if(var1 != other.var1)  
    17.         return false;  
    18.     return true;  
    19. }  

     如果子类中增加了新特性,同时保留equals方法,这时比较复杂。

    接下来我们通过实例来理解上面的约定。我们首先以一个简单的非可变的二维点类作为开始: 
    public class Point{ 
      private final int x; 
      private final int y; 
      public Point(int x, int y){ 
        this.x = x; 
        this.y = y; 
      } 

      public boolean equals(Object o){ 
        if(!(o instanceof Point)) 
          return false; 
        Point p = (Point)o; 
          return p.x == x && p.y == y; 
      } 




    假设你想要扩展这个类,为一个点增加颜色信息: 
    public class ColorPoint extends Point{ 
      private Color color; 
      public ColorPoint(int x, int y, Color color){ 
        super(x, y); 
        this.color = color; 
      } 

      //override equasl() 

      public boolean equals(Object o){ 
        if(!(o instanceof ColorPoint)) 
         return false; 
        ColorPoint cp = (ColorPoint)o; 
        return super.equals(o) && cp.color==color; 
      } 



      我们重写了equals方法,只有当实参是另一个有色点,并且具有同样的位置和颜色的时候,它才返回true。可这个方法的问题在于,你在比较一个普通点和一个有色点,以及反过来的情形的时候,可能会得到不同的结果: 
    public static void main(String[] args){ 
      Point p = new Point(1, 2); 
      ColorPoint cp = new ColorPoint(1, 2, Color.RED); 
      System.out.println(p.equals(cp)); 
      System.out.println(cp.eqauls(p)); 


    运行结果: 
    true   
    false 
    这样的结果显然违反了对称性,你可以做这样的尝试来修正这个问题:让ColorPoint.equals在进行“混合比较”的时候忽略颜色信息: 
    public boolean equals(Object o){ 
      if(!(o instanceof Point)) 
        return false; 
      //如果o是一个普通点,就忽略颜色信息 
      if(!(o instanceof ColorPoint)) 
        return o.equals(this); 
      //如果o是一个有色点,就做完整的比较 
      ColorPoint cp = (ColorPoint)o; 
      return super.equals(o) && cp.color==color; 


    这种方法的结果会怎样呢?让我们先来测试一下: 
    public static void main(String[] args){ 
      ColorPoint p1 = new ColorPoint(1, 2, Color.RED); 
      Point p2 = new Point(1, 2); 
      ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE); 
      System.out.println(p1.equals(p2)); 
      System.out.println(p2.equals(p1)); 
      System.out.println(p2.equals(p3)); 
      System.out.println(p1.eqauls(p3)); 


    运行结果: 
    true 
    true 
    true 
    false 

      这种方法确实提供了对称性,但是却牺牲了传递性(按照约定,p1.equals(p2)和p2.eqauals(p3)都返回true,p1.equals(p3)也应返回true)。要怎么解决呢?

    事实上,这是面向对象语言中关于等价关系的一个基本问题。要想在扩展一个可实例化的类的同时,既要增加新的特征,同时还要保留equals约定,没有一个简单的办法可以做到这一点。新的解决办法就是不再让ColorPoint扩展Point,而是在ColorPoint中加入一个私有的Point域,以及一个公有的视图(view)方法: 
    public class ColorPoint{ 
      private Point point; 
      private Color color; 
      public ColorPoint(int x, int y, Color color){ 
        point = new Point(x, y); 
        this.color = color; 
      } 

      //返回一个与该有色点在同一位置上的普通Point对象 
      public Point asPoint(){ 
        return point; 
      } 

      public boolean equals(Object o){ 
        if(o == this) 
         return true; 
        if(!(o instanceof ColorPoint)) 
         return false; 
        ColorPoint cp = (ColorPoint)o; 
        return cp.point.equals(point)&& 
                 cp.color.equals(color); 

      } 


      还有另外一个解决的办法就是把Point设计成一个抽象的类(abstract class),这样你就可以在该抽象类的子类中增加新的特征,而不会违反equals约定。因为抽象类无法创建类的实例,那么前面所述的种种问题都不会发生。 

    重写equals方法的要点: 
    1. 使用==操作符检查“实参是否为指向对象的一个引用”。

    2.判断实参是否为null
    3. 使用instanceof操作符检查“实参是否为正确的类型”。 
    4. 把实参转换到正确的类型。 
    5. 对于该类中每一个“关键”域,检查实参中的域与当前对象中对应的域值是否匹 
      配。对于既不是float也不是double类型的基本类型的域,可以使用==操作符 
      进行比较;对于对象引用类型的域,可以递归地调用所引用的对象的equals方法; 
      对于float类型的域,先使用Float.floatToIntBits转换成int类型的值, 
      然后使用==操作符比较int类型的值;对于double类型的域,先使用 
      Double.doubleToLongBits转换成long类型的值,然后使用==操作符比较 
      long类型的值。 
    6. 当你编写完成了equals方法之后,应该问自己三个问题:它是否是对称的、传 
      递的、一致的?(其他两个特性通常会自行满足)如果答案是否定的,那么请找到 
      这些特性未能满足的原因,再修改equals方法的代码。

  • 相关阅读:
    “”开天眼“”,天地分割效果
    关于获得当前的index的方法
    echart(2),模拟数据导入篇
    腾讯windows系统服务器
    elsarticle模板 去掉Preprint submitted to
    elsarticle模板 去掉摘要前后的两条横线
    LeetCode 345. Reverse Vowels of a String
    path变量修改后无法保存
    LeetCode 13: Roman to Integer
    LeetCode 118. Pascal's Triangle
  • 原文地址:https://www.cnblogs.com/flyingsir/p/3926776.html
Copyright © 2011-2022 走看看