Overriding the Java Equals() Method - Not Working

Overriding the java equals() method - not working?

In Java, the equals() method that is inherited from Object is:

public boolean equals(Object other);

In other words, the parameter must be of type Object. This is called overriding; your method public boolean equals(Book other) does what is called overloading to the equals() method.

The ArrayList uses overridden equals() methods to compare contents (e.g. for its contains() and equals() methods), not overloaded ones. In most of your code, calling the one that didn't properly override Object's equals was fine, but not compatible with ArrayList.

So, not overriding the method correctly can cause problems.

I override equals the following everytime:

@Override
public boolean equals(Object other){
if (other == null) return false;
if (other == this) return true;
if (!(other instanceof MyClass)) return false;
MyClass otherMyClass = (MyClass)other;
...test other properties here...
}

The use of the @Override annotation can help a ton with silly mistakes.

Use it whenever you think you are overriding a super class' or interface's method. That way, if you do it the wrong way, you will get a compile error.

Overriding equals method not working on using object as key in hashmap?

When you add new person in map personsMap.put(person,"MyCar"); first of all if key is not null the position where the element will be put is determined. It is determined by calling hashcode method for key. There are a few steps after but it doesn't matter for this example.

Since you don't override hashcode() your person and otherPerson will have different hashcode.

The same happens when you try to get value by some key. To find position where the element is, hashcode() will be invoked. But otherPerson has different hashcode and it will lead to position where is no item (null)

equals() is used when at the same position there are many elements (in list or tree structure). Then to find the right item they will be compared by equals() method

Overriding equals method doesn't work

That's because you're breaking symmetry as specified in the contract of equals(): if any event equals to "item" (which is a String), "item" should also be equal to any event.

Actually, what Java does is to call indexOf("item") on your list, and check if it is positive.

Now, indexOf() works like this in an ArrayList for instance (see complete source code here):

    for (int i = 0; i < size; i++)
if ("item".equals(elementData[i]))
return i;

So basically it is the String's equals() method which is called here, not your one which is returning false of course.

Solve this issue by simply specifying an Event parameter to the function, like:

events.contains( new Event("item", "title", "desc") )

Note that you'll have to create a proper constructor for your class ot initialize the members.

Java object equals method not work

In your case:

Right: if ( x.equals(y) )

Wrong: if ( x == y )

If the equals API is not working, then you've overridden it in your Card class, and you've implemented it wrong.

If you haven't overridden it, well then do it:

public class Card 
{
...

@Override
public boolean equals(final Object obj)
{
if (obj == this)
return true;

if (obj == null || !(obj instanceof Card))
return false;

Card otherCard = (Card) obj;

if (otherCard.score != this.score) return false;
if (otherCard.symbol != this.symbol) return false;
if (!otherCard.warna.equals(this.warna)) return false;
if (!otherCard.type.equals(this.type)) return false;
if (!otherCard.value.equals(this.value)) return false;

return true;
}

}

Overriding equals not working

It's hard to tell without more information, but it appears that your Matricola, Name and Surname fields are Objects, possibly Strings. In which case, you should compare them like this:

return Matricola.equals(s.Matricola) && Name.equals(s.Name) && Surname.equals(s.Surname);

Otherwise you are comparing references.

overloading or overriding equals() method

Overriding is when you implement a method that has been declared in a superclass. To qualify as an override, the signature of the override must match (within some tolerance) the signature of the method being overridden. ​

Overloading is when you have multiple methods with the same name but different signatures.

In the presence of overloads, which one is called is determined solely by the static types of the arguments at the call site. In your example, you have two overloads of equals:

boolean equals(Object o) { ... }
boolean equals(Person p) { ... }

When you call:

Person p = ...
... equals(p) ...

the overload selection process proceeds as follows:

  • Determine the static types of the arguments.
  • Determine which overloads are applicable (using arity, subtyping, conversion, etc.)
  • If more than one is applicable, determine the most specific one.
  • If two or more are equally specific, a compile-time error is issued.

Here, the argument type is Person, and both overloads are applicable (since a Person is an Object), but equals(Person) is more specific, so that one is called.

If we changed the story slightly:

Object p = new Person(...);
... equals(p) ...

Now, the static type of p is Object, so only the first overload -- equals(Object) -- is applicable. All we know is that it is an Object; that it happens to hold a Person is something that we don't know statically. (We could find it out dynamically with instanceof.)

To summarize:

  • Expressions have both a static (compile-time) and dynamic (run-time) type;
  • Overload selection is done purely on the basis of static types.

Now, you probably don't want to declare equals(Person) for the same problems you're seeing here -- it looks like an override, but really its an overload, and won't get called when you think it does.

Set is not working with overridden equals

You need to override hashCode as well as equals. The general contract of hashCode is that if two objects are equal (in the sense of equals), then they have the same hash code.



Related Topics



Leave a reply



Submit