• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with TreeSet and own class [Solved]

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings everyone!

My problem is the following:

I have a Node class which now represents a cell in a matrix. It implements the Comparable interface and overrides the equals(...) method because I'd like to store Node objects in a TreeSet<Node> set.



Unpredicted behaviour occurs at this point:



I'll explain it properly. openedNode and n are instances of Node, openedNodes is an instance of TreeSet<Node>.
The method g(Node, Node) in the first row of the code above just adds the g values of the two parameters, getG() and setG(int) are just regular get and set methods.

When execution reaches "openedNodes.add(openedNode);" sometimes it says that openedNodes already contains openedNode (but in reality, it doesn't contains it).

Then I tried this:


In this case it removes a Node from openedNodes (call it falseOpenedNode) and then adds the actual openedNode, so it finds that these two nodes are equivalent. But when I call openedNode.equals(falseOpenedNode) explicitly at this point it returns false (which is correct).

I'll show an example with concrete values if you need it.

Any help is appreciated! Thank you.


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

To be able to store Node objects in a TreeSet, Node must also implement the hashCode() method in accordance with your equals() method. Here's a good article that explains how you should implement equals() and hashCode(): Java theory and practice: Hashing it out
 
Tamas Gombos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply.

I've written the hashCode() and equals() methods according to the article, then I've generated these methods with Eclipse but neither of the solutuions works properly It still does the same mistake.
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tamas Gombos wrote:I've written the hashCode() and equals() methods according to the article



Could you show them..?
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your compareTo() method is inconsistent with your equals() method. That is, to use a TreeSet as a Set, for all a and b where a.compareTo(b) == 0, a.equals(b) must be true.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, TreeSet is not hash based tree, so it does not use hashcode() and equals() method to before storing the objects.
TreeSet just uses campareTo() method for this purpose. So just concentrate on your compareTo() method.

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show your example with concrete values, and try to give complete program.
 
Tamas Gombos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Garrett Rowe wrote:Your compareTo() method is inconsistent with your equals() method. That is, to use a TreeSet as a Set, for all a and b where a.compareTo(b) == 0, a.equals(b) must be true.



Okay, now I use HashSet instead of TreeSet and I implemented a method which selects the element with the minimal (g+h) value. I won't forget this case for sure Thank you!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic