Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

compareTo and equals methods consistency

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any body describe with example the following text taken by
Compareable Interface:
"
It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract
for set (or map), which is defined in terms of the equals method.
"
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Fantastic question.
After seeing your question i have gone through API.
I am very to see this question. Thanks.

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding a new Float(-0.0F) and new Float(0.0F) to HashSet would result in two values being added. Whereas on a TreeSet only one would be added. This is so because the equals would return false for the two objects whereas the compareTo would return true. Maybe an API reading would help you on this. Effective Java by Joshua Bloch is also a great read.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashid,
I have compliled a sample program just for you. because i like your question.
Here in program you will find compateTo inconsistent with equals definition.



Khudaa haafiz,

[ February 27, 2007: Message edited by: Srinivasan thoyyeti ]
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help.
You have done it and done it in BIG WAY.
I will be posting such questions in coming days, so please give them special attention.
ALLAH HAFIZ
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why It is generally a good idea for comparators to implement java.io.Serializable?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashid,
Go Ahead. Bring the best out of you.
I will try my level best to support you.
All the best
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashid,
You are doing the Good thing by going through the Collection API.

Here comes another Biggy for your question. You can easily go through the code. I have taken a TreeSet(Comparator) where comparator is not serializable.
Then point is After deserialization TreeSet Object, if you try to add anyhing it throws "Comparator is not serializble" thats why it has to be Serializable.


Lets Rock
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Your examples have jardown the in-depth detailed knowledge of "What I was looking for" in my mind.
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I replaced Comparator defination with this:

class EmployeeSalComparator implements Comparator<Employee>,Serializable {

Even then it is giving same old exception. One other thing annoying for me is WHY that print statment, after the catch block is being executed( when exception is thrown?)
Please Answer both of these questions.
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I replaced Comparator defination with this:

class EmployeeSalComparator implements Comparator<Employee>,Serializable {

Even then it is giving same old exception. One other thing annoying for me is WHY that print statment, after the catch block is being executed( when exception is thrown?)
Please Answer both of these questions.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the posted code Employee is not serializable. Since, the Set contains an Employee object, so it needs to be serializable.
Both the comparator and the Employee classes have to be serializable, else you will get the NotSerializableException.
The print statement will always be printed because from the catch block, its not throwing the exception again
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moreover, it is not about the comparators having to be serializable, its the knowledge about the objects you are serializing.
Since TreeSet uses a TreeMap and the TreeMap stores the Comparator object, so your comparator object needs to be serializable.
If you notice the exception occurs during serialization and not during the add of the element.
Any object you serialize, all the non-transient and non-static fields should be serializable.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashid,

You must be in a position to correct those little errors,like Employee must be serializable. Just i want to show you how comparator exception is thrown.

Practise more.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasan thoyyeti:
Hi Rashid,

You must be in a position to correct those little errors,like Employee must be serializable. Just i want to show you how comparator exception is thrown.

Nitesh Thanks for your corrections. Now i am also clear why exactly it has to be serializable.

Practise more.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic