aspose file tools
The moose likes Beginning Java and the fly likes Regarding Comparator and Comparable interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Regarding Comparator and Comparable interfaces" Watch "Regarding Comparator and Comparable interfaces" New topic
Author

Regarding Comparator and Comparable interfaces

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1142

Hi all

I went through the material in SCJP Exam Study Guide but couldn't quite understand the concept behind java.util.Comparator and java.lang.Comparable interfaces and how and when to use these. Can anyone please provide some material / simple code examples which shed some light on these topics for a beginner?

Regards
Mansukhdeep


~ Mansukh
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

tried google? that might help..


OCPJP 6
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Suitable Google keywords for your question would be "java comparator comparable".
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

java.lang.Comparable: int compareTo(Object o1)
This method compares this object with o1 object. Returned int value has the following meanings.
positive – this object is greater than o1
zero – this object equals to o1
negative – this object is less than o1

java.lang.Comparator: int compare(Object o1, Objecto2)
This method compares o1 and o2 objects. Returned int value has the following meanings.
positive – o1 is greater than o2
zero – o1 equals to o2
negative – o1 is less than o1



we can have only one implementation of compareTo() or Compare() method in one class


SCJP6.0,My blog Ranchers from Delhi
Virendrasinh Gohil
Ranch Hand

Joined: Jun 09, 2004
Posts: 46
shanky sohar wrote:we can have only one implementation of compareTo() or Compare() method in one class


Shanky, just to correct you. There is no compiler restriction to have both the methods in single class. A class can very well implement both the interfaces at the same time and can implement both the methods without conflict. I think you actually meant, there is no use of implementing compareTo() and compare() methods (and Comparable & Comparator interfaces) in a single class. Both the method serves same purpose but differs the way both are used.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
You would find this and this and doubtless lots more if you would search JavaRanch.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

Virendrasinh Gohil wrote:
shanky sohar wrote:we can have only one implementation of compareTo() or Compare() method in one class


Shanky, just to correct you. There is no compiler restriction to have both the methods in single class. A class can very well implement both the interfaces at the same time and can implement both the methods without conflict. I think you actually meant, there is no use of implementing compareTo() and compare() methods (and Comparable & Comparator interfaces) in a single class. Both the method serves same purpose but differs the way both are used.



I mean to say we can not have implementation of compareTo() twice in a single class
and same is for campare()
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Regarding Comparator and Comparable interfaces
 
Similar Threads
restful webservices
in jsp what tags go where after compilation?
scjp
Active vs Passive usage of classes/Interfaces
Java Interface