Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator doubt

 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java doc

Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit Comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.

For example, if one adds two keys a and b such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) to a sorted set with comparator c, the second add operation will return false (and the size of the sorted set will not increase) because a and b are equivalent from the sorted set's perspective.






Result:
DJ
DJ
Deepak


Which i think is correct because
TreeSet<Employee> listSet = new TreeSet<Employee>(new EmployeeAgeComparator());
listSet is created using AgeComparator and for AgeComparator
(new Employee("DJ", 23));
(new Employee("DJ", 24));
Are different objects . But
new Employee("DJ", 23).equals(new Employee("DJ", 24)) is true becuase Employee equals method is based on name and since names are both DJ , Hence both objects are equal . I think the result is correct.

The thing am not understanding the statement from Java doc
For example, if one adds two keys a and b such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) to a sorted set with comparator c, the second add operation will return false (and the size of the sorted set will not increase) because a and b are equivalent from the sorted set's perspective.

a.equals((Object)b) : new Employee("DJ", 23).equals(new Employee("DJ", 24)) is true
c.compare((Object)a, (Object)b) != 0): Age Comparator will return new (new Employee("DJ", 23));
(new Employee("DJ", 24));
as unequal
But the set accepted both the objects. then why does java doc speak otherwise?
Someone please clarify
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well as far as I get it, the example you gave is perfect to see why the javadoc says that the comparator should be compatible with equals. In your example you are able to add two objects to a TreeSet which are equal as per equals method, but are different according to the compare method. The output is also correct. But when you use

listSet.get(new Employee("DJ", 23));
and
listSet.get(new Employee("DJ", 24));

then the "strange behavior" that javadoc mentions comes into action. Now you have two keys which are basically the same according to the equals method. So now the get method might match the same key both the time or might match different keys both the time...
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you demonstrate
"For example, if one adds two keys a and b such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) to a sorted set with comparator c, the second add operation will return false (and the size of the sorted set will not increase) because a and b are equivalent from the sorted set's perspective."

Using the example i gave..
Further
listSet.get(new Employee("DJ", 23));
and
listSet.get(new Employee("DJ", 24));
TreeSet and Set does not support get methods.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Deepak, I gave my example according to TreeMap that is why I used the get method. Anyways you are right that if (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0), then the second add operation will return true and size of the set WILL increase. Lets see if there is any expert opinions on this...
 
siraj siddiqui
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak i think there is some mistake in the quote that you have posted.
Here is the quote that you have posted

"Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit Comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.

For example, if one adds two keys a and b such that (a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0) to a sorted set with comparator c, the second add operation will return false (and the size of the sorted set will not increase) because a and b are equivalent from the sorted set's perspective."

But when i saw the comparator interface in the java docs it is like this

" Caution should be exercised when using a comparator capable of imposing an ordering inconsistent with equals to order a sorted set (or sorted map). Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals.

For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, even though this is contrary to the specification of the Set.add method."
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what i have in my javadoc [1.5]

the second add operation will return false (and the size of the sorted set will not increase) because a and b are equivalent from the sorted set's perspective.


You have completely inverted the sentence.
 
siraj siddiqui
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Deepak but i have not invertared the sentence.Imfact that is what i have in javadocs[1.6]

The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, even though this is contrary to the specification of the Set.add method.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Deepak, in java 6.0 doc
it is "The second add operation will return true."
Now this statement will solve your problem.

But java 5.0 has same statement with false.
I think java 5 has documented this thing wrong.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well then. Thats why they say always work with latest software, and thats how enterprises can mint money in software industry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic