关于对象比较 [英] Regarding Object Comparison

查看:122
本文介绍了关于对象比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java类Rec。我有两个实例Rec1和Rec2。我想检查Rec1和Rec2的值是否相等。如果我做Rec1.equals(Rec2)是正确的做法吗?

I am having a java class Rec. I have two instance of it Rec1 and Rec2. I want to check whether the values of Rec1 and Rec2 are equal. If i do Rec1.equals(Rec2) is it correct way of doing it?

class Rec {

  private BigDecimal  RecordId = null; 

  private BigDecimal recSubNum = null; 

  private BigDecimal  FileId = null;

  private String    Category = null; 

  private BigDecimal status = null; 

  private BigDecimal errorCode = null; 

} 


推荐答案

实现在Java中实现对象相等的 equals() hashCode()方法:

You need to implement the equals() and hashCode() methods to implement object equality in Java:

class Rec {
  private BigDecimal recordId = null;
  private BigDecimal recSubNum = null;
  private BigDecimal FileId = null;
  private String category = null;
  private BigDecimal status = null;
  private BigDecimal errorCode = null;

  @Override
  public int hashCode() {
    int ret = 41;
    ret = hc(ret, recordId);
    ret = hc(ret, recSubNum);
    ret = hc(ret, fieldId);
    ret = hc(ret, category);
    ret = hc(ret, status);
    ret = hc(ret, errorCode);
    return ret;
  }

  @Override
  public boolean equals(Object ob) {
    if (ob == null) return false;
    if (ob.getClass() != Rec.class) return false;
    Rec r = (Rec)ob;
    if (!eq(r.recordId, record)) return false;
    if (!eq(r.recSubNum, recSubNum)) return false;
    if (!eq(r.fileId, fileId)) return false;
    if (!eq(r.category, category)) return false;
    if (!eq(r.status, status)) return false;
    if (!eq(r.errorCode, errorCode)) return false;
    return true;
  }

  private static boolean eq(Object ob1, Object ob2) {
    return ob1 == null ? ob2 == null : ob1.equals(ob2);
  }

  private static int hc(int hc, Object field) {
    return field == null ? hc : 43 + hc * field.hashCode();
  }
}

注意: / hashCode for Java意味着对于任何两个对象a和b:

Note: the equals/hashCode contract for Java means that for any two objects a and b:

a.equals(b) == b.equals(a)

如果两个对象相等,则 a.hashCode

and if two objects are equal then a.hashCode() must equal b.hashCode().

编辑:有两种方法检查类型是否匹配。

there are two ways of checking if the types match. Either:

if (ob == null) return false;
if (ob.getClass() != Rec.class) return false;

if (!(ob instanceof Rec)) return false;

这两个做不同的事情,你应该根据你想做什么选择正确的。我一般喜欢第一个,除非你知道你需要第二个。有什么区别?

These two do different things and you should select the correct one depending on what you want to do. I generally prefer the first one unless you know you need the second. What's the difference?

class A {
  public int i;

  public boolean equals(Object ob) {
    if (!(ob instanceof A)) return false;
    return i == ((A)ob).i;
  }
}

看起来合理吗?如果类得到扩展该怎么办:

Looks reasonable right? What if the class gets extended:

class B extends A {
  public int j;

  public boolean equals(Object ob) {
    if (!(ob instanceof B)) return false;
    if (!super.equals(ob)) return false;
    return j == ((B)ob).j;
  }
}

仍然合理吗?

A a = new A();
a.i = 10;
B b = new B();
b.i = 10;
b.j = 20;
System.out.println(a.equals(b)); // true! Is this really what you want?
System.out.println(b.equals(a)); // false! Different to previous = problem.

这就是为什么我喜欢 getClass() instanceof ,除非我真的想要子类别相等。

That's why I favour getClass() over instanceof unless I really want subclass equality.

这篇关于关于对象比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆