使用equals方法比较两个对象,Java

4
我有一个对象数组,想要将其与目标对象进行比较。我想返回与目标对象完全匹配的对象数量。
这是我的计数方法:
public int countMatchingGhosts(Ghost target) {
        int count=0;
        for (int i=0;i<ghosts.length;i++){
            if (ghosts[i].equals(target));
            count++;
        }
        return count;

这是我的equals方法:
public boolean equals(Ghost other){
           if(this == other) return true;
           if( !(other instanceof Ghost) ) return false;
           Ghost p = (Ghost)other;

        if (this.x == p.x && this.y == p.y && this.direction==p.direction && this.color.equals(p.color))
            return true;
        else
            return false;

我运行了一些测试代码,我只期望得到1个匹配,但实际上得到了3个。你看到有什么错误吗?

1
Ghost有哪些字段,以及Ghost如何初始化?我认为该数组具有非唯一值。 - Bivas
3个回答

16

你的 if 语句末尾有一个 ;

if (ghosts[i].equals(target));
                             ^

这将使count ++;始终发生,而不管您的equals方法返回什么。


5
这就是为什么我总是使用牙套。有一次我找了两天才找到这样的东西。再也不想再经历那种痛苦了... - hvgotcodes

3
您应该重写此函数:
public boolean equals(Object other) { }

请注意方法签名中使用的是 Object 类而不是 Ghost。如果您没有正确使用方法签名,可以使用 @Override 注释来获得编译器错误提示。

@Override
public boolean equals(Object other) { }

说了这么多,你代码中可能正在发生的情况就是其他答案所述的情况...

0

在您的代码中实现equals方法时,必须同时实现(重写)hashCode方法。这是您必须遵循的通用约定,以获得最佳性能。

以下是Joshua Bloch的书《Effective Java》的摘录:

第9项: 在重写equals时总是要重写hashCode

A common source of bugs is the failure to override the hashCode method. You
must override hashCode in every class that overrides equals. Failure to do so
will result in a violation of the general contract for Object.hashCode, which will
prevent your class from functioning properly in conjunction with all hash-based
collections, including HashMap,HashSet, and Hashtable.
Here is the contract, copied from the Object specification [JavaSE6]:
• Whenever it is invoked on the same object more than once during an execution
  of an application, the hashCode method must consistently return the
  same integer, provided no information used in equals comparisons on the
  object is modified. This integer need not remain consistent from one execution
  of an application to another execution of the same application.
• If two objects are equal according to the equals(Object) method, then calling
  the hashCode method on each of the two objects must produce the same
  integer result.

就像Pablo所说的那样,如果你在equals方法签名中使用除了Object类以外的任何东西,你实际上并没有覆盖equals方法,你的程序将无法按预期工作。

以这个小程序为例,它将一个List复制到一个Set(不能包含重复项)并打印新的集合。尝试用equals(Item obj)替换equals(Object obj),看看运行程序时会发生什么。此外,注释掉hashCode()方法并运行程序,观察使用和不使用它之间的差异。

public class Item {
      private String name;
      private double price;
      private String countryOfProduction;

public Item(String name, double price, String countryOfProduction) {
    this.setName(name);
    this.setPrice(price);
    this.setCountryOfProduction(countryOfProduction);
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public String getCountryOfProduction() {
    return countryOfProduction;
}

public void setCountryOfProduction(String countryOfProduction) {
    this.countryOfProduction = countryOfProduction;
}

public String toString() {
    return "Item Name: " + getName() + "\n" +
            "Item Price: N" + getPrice() + "\n" +
            "Country of Production: " + getCountryOfProduction() + "\n";
}

@Override
public boolean equals(Object obj) {
    if(!(obj instanceof Item)) {
        return false;
    }
    if(obj == this) {
        return true;
    }

    Item other = (Item)obj;
    if(this.getName().equals(other.getName())
              && this.getPrice() == other.getPrice() 
              && this.getCountryOfProduction().equals(other.countryOfProduction)) {
        return true;
    } else {
        return false;
    }

}

public int hashCode() {
    int hash = 3;

    hash = 7 * hash + this.getName().hashCode();
    hash = 7 * hash + this.getCountryOfProduction().hashCode();
    hash = 7 * hash + Double.valueOf(this.getPrice()).hashCode();
    return hash;

}

public static void main (String[]args) {

    List<Item> items = new ArrayList<>();


    items.add(new Item("Baseball bat", 45, "United States"));
    items.add(new Item("BLUESEAL Vaseline", 1500, "South Africa"));
    items.add(new Item("BLUESEAL Vaseline", 1500, "South Africa"));


    Collection<Item> noDups = new HashSet<>(items);


    noDups.stream()                      
            .forEach(System.out::println);
    }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接