• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Calling generics .equals method problem

 
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?
 
author
Posts: 23843
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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:
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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: 10777
71
Hibernate Eclipse IDE 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
 
I'm still in control here. LOOK at this tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!