高效的equals(Object o)实现 [英] An efficient equals(Object o) implementation

查看:62
本文介绍了高效的equals(Object o)实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写完标题后,仍然读了这样的帖子决定解决有关Java中相等的防错实现的问题.这是我的正常执行方式

I read this SO post after I wrote out the title but still decided to go through with the question on bug-proof implementations of equals in Java. This is my normal implementation

@Override
        public boolean equals(Object o){
            if(o == null) return false;
            if(o instanceof CompositePk == false) return false;
            if(this == o) return true;
            CompositePk that = (CompositePk)o;
            return new EqualsBuilder().append(this.id, that.id)
                                      .append(this.bucketId, that.bucketId)
                                      .isEquals();
        }

使用Apache的EqualsBuilder进行平凡的工作.比这更容易的是我的Netbean自动生成的 equals(o)实现

using Apache's EqualsBuilder to do the mundane stuff. Even easier than this is my Netbean's automatically generated equals(o) implementation

 @Override
        public boolean equals(Object obj){
        if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final TemplatesWrapper other = (TemplatesWrapper) obj;
            if (this.timeAdded != other.timeAdded && (this.timeAdded == null || !this.timeAdded.equals(other.timeAdded))) {
                return false;
            }
            return true;
    }

我从2个diff项目中获取了这些内容,但是它们都尝试使用diff方法来完成相同的事情.您想选择哪种样式,或者发现任何缺陷?

I take these from 2 diff projects but they both try to accomplish the same thing but using diff approaches. Which style would you rather or are there any flaws you spot?

推荐答案

我会这样做:

public boolean equals(Object ob) {
  if (ob == null) return false;
  if (ob == this) return true;

  if (!(ob instanceof MyClass)) return false; // OR
  if (ob.getClass() != getClass()) return false;

  // check relevant members
}

中间的两行不同.一个允许子类相等(第一个),另一个不允许.使用适当的一种.

The two lines in the middle are different. One allows for subclasses to be equal (the first one), the other doesn't. Use whichever one is appropriate.

举个例子,Java的 AbstractList 类可能会使用第二种形式,因为 List 的确切实现是无关紧要的.重要的是成员是否相等且位置相同.

To give you an example, Java's AbstractList class will probably use the second form, because the exact implementation of List is irrelevant. what matters is if the members are equal and in the same position.

相反,Person类应使用第一种形式(instanceof),因为如果存在Student子类,并且您调用 Person.equals(Student),则它可能返回true而不检查Student中的额外字段而Student.equals(Person)可能会返回 false .如果 equals()不是可交换的,那么您就是在寻求麻烦.

Conversely, a Person class should use the first form (instanceof) because if there is a Student subclass and you call Person.equals(Student) it may return true without checking the extra fields in Student whereas Student.equals(Person) will probably return false. If equals() isn't commutative, you're asking for trouble.

我倾向于使用由我的IDE(IntelliJ IDEA)生成的 equals()方法,而不是对某些Apache库创建不必要的依赖关系.

I tend to use equals() methods generated by my IDE (IntelliJ IDEA) rather than creating an unnecessary dependency to some Apache library for little gain.

这篇关于高效的equals(Object o)实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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