• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling generics .equals method problem  RSS feed

 
Mitch Aman
Greenhorn
Posts: 9
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im writing code for a hash table, and i came across a weird problem.
I have a very basic class EntrySet<K,V> that simply holds K key, and V value.
I have a generic Linked List Class, which is going to be holding my EntrySet class.

The problem lies in the .contains() method of my LinkedList. When im trying to call the .equals() of EntrySet it's never reached.

.contains():


EntrySet's .equals():


The line "System.out.println(key + "equals" + set.key + "??");" is never printed, so I know the .equals() is not being called. Why is this?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mitch Aman wrote:
The line "System.out.println(key + "equals" + set.key + "??");" is never printed, so I know the .equals() is not being called. Why is this?


The same technique that you used to determine that equals() is not being called, can be used to determine, when the loop is entered, whether the caller method has been called, etc. In other words, you can use the same trick to determine, why yourself.

Henry
 
Mitch Aman
Greenhorn
Posts: 9
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Mitch Aman wrote:
The line "System.out.println(key + "equals" + set.key + "??");" is never printed, so I know the .equals() is not being called. Why is this?


The same technique that you used to determine that equals() is not being called, can be used to determine, when the loop is entered, whether the caller method has been called, etc. In other words, you can use the same trick to determine, why yourself.

Henry


Thanks for the response Henry, I have tried that previously, and had no luck.


Prints:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mitch Aman wrote:The line "System.out.println(key + "equals" + set.key + "??");" is never printed, so I know the .equals() is not being called. Why is this?

Because the equals() method in Object (which is the one that contains() calls, and the one that you should be overriding) is NOT generic. Is is specifically:
public boolean equals(Object o) { ...

Tip: Whenever you override a method, add the
@Override
annotation. This would have generated a compile error, because
public boolean equals(EntrySet<K,V> set) { ...
does NOT override anything.

HIH

Winston
 
Mitch Aman
Greenhorn
Posts: 9
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mitch Aman wrote:The line "System.out.println(key + "equals" + set.key + "??");" is never printed, so I know the .equals() is not being called. Why is this?

Because the equals() method in Object (which is the that contains() calls) is NOT generic. Is is specifically:
public boolean equals(Object o) { ...

Tip: Whenever you override a method, add the
@Override
annotation. This would have generated a compile error, because
public boolean equals(EntrySet<K,V> set) { ...
does NOT override anything.

HIH

Winston


DOH, that did it.
Thank you very much.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mitch Aman wrote:DOH, that did it.
Thank you very much.

You're most welcome; it trips up LOTS of people.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!