• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Comparator and Comparable interfaces  RSS feed

 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tried google? that might help..
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suitable Google keywords for your question would be "java comparator comparable".
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would find this and this and doubtless lots more if you would search JavaRanch.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!