• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeSet and Comparator  RSS feed

 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am currently working with a book and I came up with a strange behavour of my code. Please I will be glad if some one explains where I lack the understanding.










Up to this point I understand well enough . No doubt at all. Now I create another class:


Problem 1: If i rearrange like below then only rectangle area are shown. Circle do not get added to the set.


Problem 2: I don't get how the below line working because GeomatricObjectComparator() should take 2 objects for comparing but how the comparison working here when we keep adding object to the set


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a bug in your comparator. It reports equals in some cases where it shouldn't -- hence, it breaks the tree set.

Henry
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry I got that bug but can you please clarify my second question please
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GeomatricObjectComparator only has a default constructor, which takes no parameters.
It has a single method, which is the implementation of compare() from the interface, and that takes two GeometricObject objects.  That is what is used by the TreeSet code to sort the entries.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The comparator you specified in the constructor is used by the add() method to see if there are any existing elements in the set that are "equal" to the object you are trying to add. If the comparator returns 0 when comparing any existing element to the new element, then the new element will not be added.

This means that if you fix the bug in your comparator, the TreeSet will reject Rectangle(1, 4) if you already have added Rectangle(2, 2) or Rectangle (4, 1) previously.  You will also be unable to add a second Circle(40) to the set.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever the TreeSet needs to compare two GeometricObjects, it simply invokes the 'compare' method of your comparator.
Have you tried to insert two GO's with the same area? For instance, Rectangle(4, 5) and Rectangle(2, 10)?
 
M Hasan
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave Tolls & Junilu Lacar. I belief now I understand. One more thing to clarify: So if you implement Comparator, does compare method always implicitly called?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use coloured text, which is difficult to read.
The compare method is explicitly called by the inserting method in the tree set, assuming you have a Comparator at all. Otherwise the elements will have a compareTo method (see Comparable) for the tree set to use.
 
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
M Hasan wrote:One more thing to clarify: So if you implement Comparator, does compare method always implicitly called?


If you assign a Comparator to the TreeSet, then any time the TreeSet needs to compare two elements, it will always use that Comparator.

Henry
 
Anton Golovin
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Hasan,

Imperative to have compareTo method of the Comparable agree with the equal method of the object in expressing the equality of two objects being compared. In fact, use the equals method returning true to return 0 by the compareTo method, best way to do it.

Same logic for Comparator.

This is imperative because the TreeSet is both a binary Tree and a Set (unique values). So it needs to be able to order the objects for proper tree-balancing and object ordering within the Tree, and also to ensure uniqueness to maintain Set semantics.



With best regards,

Anton.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!