• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Comparator Syntax

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Just wanted to make sure I am fine with Comparator syntax. The method in a class that implements Comparable/Comparator interface should be something like this ??

<ref variable>.<method Name> - <ref variable>.<method Name> for primitives and
<ref variable>.<method Name>.compareTo(<ref variable>.<method Name>) for String/Wrapper classes right ?

In the case of comparator, Here is a sample code for primitivessample Code for String typesAlso, In the case of Comparable, why compareTo() method is coming twice ?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also, In the case of Comparable, why compareTo() method is coming twice ?


It is coming twice because you are defining the way tow Employee objects have to be compared.
And for that, tou rely on comparing their name (which is string). And String class implements Comparable<String> which means this class has implemented compareTo method.
(javadoc 1.6)
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think for primitives, we have to use -(minus) to check the value(negative or positive or equal) and NOT compateTo() method. For non-primitives use, compareTo() method and not -(minus). String and Wrappers classes implemented this compareTo() method. This syntax looks good to me. Maybe somebody can point out if there is a mistake.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!