wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What does this mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What does this mean?" Watch "What does this mean?" New topic
Author

What does this mean?

Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 207

"The CompareTo() method is not consistent with equalsTo() method".......What Does This Mean ??? &

Why Do we need CompareTo() method to be Consistent with EqualsTo() in our program???.....What is the relation Between The TWO ???

please Help Me And Make me Understand .....I Would Appreciate you all....


OCJP-90%,OCPWCD-95%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40


We really need more context than that.... but if I had to take a guess, could it be that you have a class where the equals() method says it is equal, but the compareTo() methods says that it is not? Or vice versa?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 207

Henry Wong wrote:
We really need more context than that.... but if I had to take a guess, could it be that you have a class where the equals() method says it is equal, but the compareTo() methods says that it is not? Or vice versa?

Henry




# import java.util.*;
# public class HashIt implements Comparable<HashIt>
# {
# String name;
# HashIt(String a)
# {
# name = a;
# }
#
# public String toString()
# {
# return name;
# }
#
# public int compareTo(HashIt n)
# {
# if(this.name.length()==n.name.length())
# return 1;
# return 0;
# }
# public static void main(String args[])
# {
# HashIt a2 = new HashIt("bb");
# HashIt a1 = new HashIt("aa");
# HashIt a3 = new HashIt("cc");
# HashIt a5 = new HashIt("aa");
# HashIt a6 = new HashIt("ee");
# HashIt a4 = new HashIt("dd");
# Map<HashIt, String> hasht = new TreeMap<HashIt, String>();
# hasht.put(a1,"AA");
# hasht.put(a2,"BB");
# hasht.put(a3,"CC");
# hasht.put(a5,"AA");
# hasht.put(a4,"DD");
# hasht.put(a6,"EEE");
# System.out.println(hasht);
#
# hasht.remove(a2); //Line-1
# System.out.println(hasht);
#
# }
# }


As in One of The Forums i saw this Program and someone said that "The keys you are using are not consistent with equals"...

Now according To my Knowledge compareTo () is used TO sort when we implement Comparable ...right ?? .....Please Explain me First of all what do you mean by the quote ""The keys you are using are not consistent with equals"" ....

And Secondly ?Why Do we Need CompareTo() and Equals () to be Conisistent ??.....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

sagar shroff wrote:
Now according To my Knowledge compareTo () is used TO sort when we implement Comparable ...right ?? .....Please Explain me First of all what do you mean by the quote ""The keys you are using are not consistent with equals"" ....


I already did. Please reread my last post.

sagar shroff wrote:
And Secondly ?Why Do we Need CompareTo() and Equals () to be Conisistent ??.....


Actually, you don't. You also don't need to implement equals() or compareTo() method at all, if you don't intend to ever use instances of your object to be used as keys in any maps or sets.

But... to answer your question. This is a requirement of the TreeMap class, and is actually mentioned in the JavaDoc. The Map interface requires that the equals() method be used to determine equality -- and to not allow duplicates. The TreeMap class which IS-A Map uses comparable (or the comparator) to determine equality, because it is using it anyway. And is assuming that those two methods are consistent.

If it is not, then the way TreeMap detects duplicates will be different from how HashMap detects duplicates. Of course, as mentioned, this is moot, if you don't care about this.

Henry
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


CompareTo() and equals() must be consistent it means that if two objects are equal with equals() method there compareTo() method must return zero. if equals() and compareTo() behave differently for equals() object then certain set can contain duplicates.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 207

Hey Henry Thanks a lot....
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 207

Javin Paul wrote:
if equals() and compareTo() behave differently for equals() object then certain set can contain duplicates.


What do you mean by certain set can contain duplicate.....Which Set??...can you mention those........Do you mean that if equals () and compareTo() is not conistent then Every type of Set will contain duplicate ....please mention Javin ??
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3574
    
  14

TreeSet, for instance. If compareTo() is not consistent with equals, then a TreeSet may think that two equal objects are different, because it uses the compareTo() method to compare objects, not the equals() method. You can end up with duplicates in your set.
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 207

Stephan van Hulst wrote:TreeSet, for instance. If compareTo() is not consistent with equals, then a TreeSet may think that two equal objects are different, because it uses the compareTo() method to compare objects, not the equals() method. You can end up with duplicates in your set.



Thanks Stephan....But what about HashSet,and others
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3574
    
  14

HashSet does not use the compareTo() method, so you don't have to worry about it.

As far as I know, the only classes that require compareTo() to be consistent with equals are TreeSet and TreeMap.
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 207




Thanks Stephan.....really appreciate you for helping Me
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Sagar...In addition to the very good explanations already given, I SERIOUSLY recommend you to do a lot of coding...lots of 'trial-and-error' coding, it may even help you get better understanding... this topic generics and collections is NOT a days job to understand...Read the API anytime you have doubts...code code code...

Regards

Ikpefua


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

Stephan van Hulst wrote:TreeSet, for instance. If compareTo() is not consistent with equals, then a TreeSet may think that two equal objects are different, because it uses the compareTo() method to compare objects, not the equals() method. You can end up with duplicates in your set.


Yes that's correct , meaning of "certain sets" was for those who use CompareTo() for checking equality and as Stephan mentioned its only TreeSet and and TreeMap in JDK but it could be other custom implementation of SortedSet also which may use CompareTo() for checking equality.

By the way Thanks Stephan
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 207

Ikpefua Jacob-Obinyan wrote:@Sagar...In addition to the very good explanations already given, I SERIOUSLY recommend you to do a lot of coding...lots of 'trial-and-error' coding, it may even help you get better understanding... this topic generics and collections is NOT a days job to understand...Read the API anytime you have doubts...code code code...

Regards

Ikpefua



Yeah i am already doing a lot of hard work from the last past month or so.I wanna do good in SCJP 6.As i am new to JAVA so it takes me little more time to understand some concepts.Anyways thanks JACOB.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does this mean?
 
Similar Threads
s.compareTo()
strings doubt
Comparable vs Comparator ??
equals() in subclasses
Die Aufgabe des Ubersetzers or help wanted