How to implement CompareTo method in Java

Tutorials Shared by the Internet Community

Advertisements


Synopsis

CompareTo method is defined in Comparable interface in Java and used to implement natural ordering of object.Implementing and overriding compareTo is

Statistics

  • Total Hits - 15381

  • Total Votes - 97 votes

  • Vote Up - 41 votes

  • Vote Down - 56 votes

  • Domain - javarevisited.blogspot.com

  • Category - JAVA/Core JAVA

  • Submitted By - Javin Paul

  • Submitted on - 2011-11-15 15:11:31

Description

CompareTo method is defined in Comparable interface in Java and used to implement natural ordering of object.Implementing and overriding compareTo is neither too easy but not too difficult as well , there are certain things we need to keep in mind e.g. compareTo must be consistent with equals method, not using subtraction for comparing two integers etc. This article explains correct way of implementing compareTo in Java. ...


Site  |   Broken  |   Tweet  |   Facebook  |     |   Save  |   Liked  |   Down

Advertisements