简体   繁体   English

为什么我的equals方法不起作用?

[英]Why isn't my equals method working?

I'm using an ArrayList , and at one point in the program I'm using the contains method to check if a certain item is contained in the ArrayList . 我正在使用一个ArrayList ,并且在程序中的某一点我正在使用contains方法来检查ArrayList是否包含某个项目。 The ArrayList holds objects of type CharProfile , a custom class, and it's seeing if a char is contained within it. ArrayList保存CharProfile类型的CharProfile ,这是一个自定义类,它会查看其中是否包含char。

So it's using the equals method in the contains method, I assume. 所以我假设它在contains方法中使用equals方法。 So something like CharProfile.contains(char) , but it's not working. CharProfile.contains(char)这样的东西,但它不起作用。

I overrode the equals method in CharProfile: 我在CharProfile中覆盖了equals方法:

@Override
public boolean equals(Object o) {
    if (this.character == (Character)o) {
        return true;
    }
    else {
        return false;
    }
}

So it should be using my equals method when CharProfile is trying to use it, right? 因此,当CharProfile尝试使用它时,它应该使用我的equals方法,对吧? So why won't it work? 那么为什么它不起作用呢?

(In terms of "not working" I'm referring to the fact that contains always returns false.) (就“不工作”而言,我指的是contains始终返回false的事实。)

You are comparing a reference type using ==, which is wrong. 您正在使用==比较引用类型,这是错误的。 You must use equals , with proper null -checks added. 您必须使用equals ,并添加适当的null -checks。

But this is just the beginning. 但这只是一个开始。 Your main problem is that you are trying to compare a CharProfile object to a Character object. 您的主要问题是您正在尝试将CharProfile对象与Character对象进行比较。 You probably need this instead: 你可能需要这个:

public boolean equals(Object o) {
  return o instanceof CharProfile 
     && this.character.equals((CharProfile)o).character;
}

This assumes that your character field is never null. 这假设您的character字段永远不为空。 If it can be null, you need to check that before dereferencing it, as well. 如果它可以为null,则还需要在取消引用之前检查它。

你重写equals,以便测试引用的相等性,运算符的默认行为==

You need to use equals (). 你需要使用equals ()。 You can also make it a oneliner and be more explicit in your cast. 你也可以使它成为一个oneliner并在你的演员阵容中更明确。

@Override
public boolean equals(Object o) {
    return o instanceof Character && this.character.equals(Character.class.cast(o));
}

You have to use the equals() method and DO NOT forget to override the hashCode() method as well. 您必须使用equals()方法,并且不要忘记覆盖hashCode()方法。 They go hand in hand. 它们齐头并进。

Some people don't know this, but if using eclipse you can right click choose Source-> and Generate hashCode() and equals()... 有些人不知道这一点,但如果使用eclipse,你可以右键单击选择Source->并生成hashCode()和equals()......

But, I suggest that you learn what they're for first before using this convenience. 但是,我建议你在使用这种便利之前先了解它们的用途。

For example, You have CharProfile as below. 例如,您有CharProfile ,如下所示。

List<CharProfile> list = new ArrayList<CharProfile>();
list.add(new CharProfile('a'));
list.add(new CharProfile('b'));
list.add(new CharProfile('c'));

When list.contains('a') is does, the JVM will not call Override equals() method of CharProfile . list.contains('a')确实存在时,JVM将不会调用CharProfile Override equals()方法。

For more clear; 为了更清楚;

public class Data {
    public boolean equals(Object o) {
        System.out.println("I am data");
        return false;
    }
}

When list.contains(new Data()) is does, the JVM will not call Override equals() method of Data . list.contains(new Data())是呢,JVM将不会调用Override equals()方法Data Now, You will get message like I am data. 现在,你会得到像我是数据的消息 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM