Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java Comparable Example

  1. /*
  2. Java Comparable example.
  3. This Java Comparable example describes how java.util.Comparable
  4. interface is implemented to compare user defined classe's objects.
  5. */
  6.  
  7. import java.util.*;
  8.  
  9. /*
  10. Comparable interface is used to make user defined class's objects comparable.
  11. This interface declares one method compareTo(Object object)
  12. and determines how objects can be compared to with each other.
  13. */
  14.  
  15. /*
  16. Implement java.util.Comparable interface to make class objects comparable.
  17. */
  18.  
  19. class Employee implements Comparable{
  20.    
  21.     private int age;
  22.    
  23.     public void setAge(int age){    
  24.         this.age=age;
  25.     }
  26.    
  27.     public int getAge(){    
  28.         return this.age;    
  29.     }
  30.    
  31.     /*
  32.    
  33.     Signature of compareTo method is,    
  34.     public int compareTo(Object object).
  35.    
  36.     compareTo method should return 0 if both objects are equal,
  37.     1 if first grater than other and -1 if first less than the
  38.     other object of the same class.
  39.    
  40.     */
  41.    
  42.     public int compareTo(Object otherEmployee){
  43.    
  44.         /*
  45.         If passed object is of type other than Employee, throw ClassCastException.
  46.         */
  47.        
  48.         if(!(otherEmployee instanceof Employee)){
  49.             throw new ClassCastException("Invalid object");
  50.         }
  51.        
  52.         int age = ((Employee) otherEmployee).getAge();
  53.        
  54.         if(this.getAge() > age)    
  55.             return 1;
  56.         else if ( this.getAge() < age )
  57.             return -1;
  58.         else
  59.             return 0;
  60.    
  61.     }
  62.  
  63. }
  64.  
  65. public class JavaComparableExample{
  66.    
  67.     public static void main(String args[]){
  68.        
  69.         /*
  70.         Create two different Employee objects, so that we can compare them.
  71.         */
  72.        
  73.         Employee one = new Employee();        
  74.         one.setAge(40);
  75.        
  76.         Employee two = new Employee();        
  77.         one.setAge(30);
  78.        
  79.         /*
  80.         Use compareTo method to determine which employee is younger
  81.         */
  82.        
  83.         if(one.compareTo(two) > 0) {        
  84.             System.out.println("Employee one is elder than employee two!");
  85.        
  86.         } else if(one.compareTo(two) < 0) {        
  87.             System.out.println("Employee one is younger than employee two!");        
  88.        
  89.         } else if(one.compareTo(two) == 0) {        
  90.             System.out.println("Both employees are same!");        
  91.         }
  92.    
  93.     }
  94.  
  95. }
  96.  
  97. /*
  98. OUTPUT of the above given Java Comparable Example would be:
  99. Employee one is elder than employee two!
  100. */
Your rating: None Average: 4.2 (27 votes)
Share this