aspose file tools
The moose likes Java in General and the fly likes comparator interface has equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "comparator interface has equals method" Watch "comparator interface has equals method" New topic
Author

comparator interface has equals method

sudhakarc kumar
Greenhorn

Joined: Feb 23, 2011
Posts: 4
i have a question that why comparator interface has equals() method in spite that every class get it by default from Object class.Please help me .Thanks in advance.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
Because they wanted to provide additional JavaDoc comments about how to override equals for a Comparator.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32611
    
    4
. . . and welcome to the Ranch
sudhakarc kumar
Greenhorn

Joined: Feb 23, 2011
Posts: 4
thanks for your reply Campbell and Mike . you mean to say that equals() method from Comparator could have been eliminated but it is just a reminder that in this case we can override equals() and how should we override it. Please explain it a little bit more because i dont think that in java there are things that have no reason. Everything thing contains some special meaning.Thanks again.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3044
    
    1

Several interfaces do this. They "override" a method declaration in a super-interface, just to expand on the general contract of the method.

A method's contract is at least as important as the method's implementation, if not more. Extending an interface for this reason is perfectly valid.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
sudhakarc kumar wrote:Please explain it a little bit more because i dont think that in java there are things that have no reason. Everything thing contains some special meaning.Thanks again.
The reason you've been given is the only reason. All interfaces automatically have a toString method included in them (JLS Section 6.4.4), so the only reason for explicitly including it in an interface is so that you can expand the Javadoc.


Joanne
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
sudhakarc kumar wrote:Please explain it a little bit more because i dont think that in java there are things that have no reason.

Ah, to be young again!
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

In common java objects, we will decide two objects are equal by checking its one or more than one instance variables.

Form javaDoc Comparator
return true -> only if the specified object is also a comparator and it imposes the same ordering as this comparator.
Means we need to check for its business logic (This is what really makes two comparators objects equal)

Also from JavaDoc
Note that it is always *safe not* to override Object.equals(Object).

Since the POJO class can have its own equals() which would check for some of its instance variables(Which makes it really unique). If we implement comparator interface in the same POJO class and provide the equals () for comparator then we cannot check equality for POJO class, vice versa. That is the reason it mentioned it's not to override equals().

As explained earlier this is to 'just to expand on the general contract of the method'. So that we would treat comparator equals() is different from POJO equals() .


A.A.Anbarasu
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: comparator interface has equals method
 
Similar Threads
Comparators
Comparator interface define equals method
i have not override comparator's equals() method(overrided compare())-working fine-how?
Difference between Comparable and Comparator
Relation b/w Object class and Interface