简体   繁体   English

Java:比较对象值

[英]Java: compare object values

I am trying to compare the value of 2 instances of x inside an iterator. 我正在尝试比较迭代器中x的2个实例的值。

x is a reference type containing its own data members. x是包含其自己的数据成员的引用类型。 I am trying to compare one instance of x against another to determine if the values inside each are the same. 我试图将x的一个实例与另一个实例进行比较,以确定每个实例中的值是否相同。

if (x.equals(x)) 如果(x.equals(x))

keeps evaluating to true when actually the value of each instance of x is different. 当x的每个实例的值实际上不同时,保持评估为true。

Cheers. 干杯。

Assuming your code doesn't really look like this 假设您的代码看起来并非如此

X x = new X();
if(x.equals(x))

but more like 但更像

X x = new X();
X y = new X();

if(x.equals(y)) { }

And you are getting wrong values for x.equals(y) then your implementation of equals is broken. 而且x.equals(y)值错误,那么equals的实现就被破坏了。

Go to your Class X and check how equals is implemented. 转到您的Class X并检查如何实现平等。 If it is not implemented in X check in the super class. 如果未在X中实现,请检查超级类。

This is a hard question to answer with the details given. 这是一个很难回答的细节问题。 They have to be objects and not primitives to have a .equals method. 它们必须是对象而不是原始对象才能具有.equals方法。 So has the equals method been overridden in a way that is causing faulty comparisons to be done? 那么是否以导致错误比较的方式重写了equals方法? That would be the place that I would be looking at. 那就是我要看的地方。

It sounds like you are comparing references and not data. 听起来您正在比较参考而不是数据。

EDIT: From the API doc for Object: "The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). " 编辑:从对象的API文档:“类对象的equals方法在对象上实现了最有区别的对等关系;也就是说,对于任何非空引用值x和y,当且仅当x和y引用相同的对象(x == y的值为true)。”

eg If 'X' or it's parent classes do not override equals then when you call equals then it will be comparing the references, which if they are the same object will always be the equal. 例如,如果“ X”或其父类未覆盖equals,则当您调用equals时,它将比较引用,如果它们是同一对象,则它们将始终相等。

By the sounds of it you need to override the equals method in the class 'X', but then again, what you say seems to indicate that they are the same reference anyhow? 听起来,您需要覆盖类'X'中的equals方法,但是,您所表示的似乎再次表明它们是相同的引用吗?

The equals method in the Object class performs an instance-equals operation. Object类中的equals方法执行实例等于操作。 it is common, however, for subclasses of Object to override this method to perform a relative-equals operation, where the values within the target object are tested against the values of the object in question. 但是,常见的是, Object子类重写此方法以执行相对等式运算,其中将目标对象内的值与相关对象的值进行测试。 In your case, you are most likely using a subclass of Object that has overriden the equals method. 在您的情况下,您很可能使用Object的子类,该子类已覆盖equals方法。 It would be good to know how the equals method of your class X is implemented. 最好知道类Xequals方法是如何实现的。

I'm going to assume that your two X objects are actually different objects, and not mistakenly the same object. 我将假设您的两个X对象实际上是不同的对象,而不会错误地是同一对象。 In this case, the problem is the equals() implementation for that class. 在这种情况下,问题在于该类的equals()实现。 Check your class and its superclasses for the equals() implementation, and make sure its actually comparing intelligently. 检查您的类及其超类是否具有equals()实现,并确保对其进行实际的智能比较。 If necessary, write your own. 如有必要,请自己编写。

class Square
{
    public int length;

    public boolean equals( Square s )
    {
        return this.length == s.length;
    }
}

如果您决定覆盖equals( ),也要覆盖hashcode()

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

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