Friday, September 18, 2015

Java Comparable interface example




 package CollectionFramework;  
 public class OFruit implements Comparable<OFruit> {  
      String name;  
      String colour;  
      int quantity;  
      public String getName() {  
           return name;  
      }  
      public void setName(String name) {  
           this.name = name;  
      }  
      public String getColour() {  
           return colour;  
      }  
      public void setColour(String colour) {  
           this.colour = colour;  
      }  
      public int getQuantity() {  
           return quantity;  
      }  
      public void setQuantity(int quantity) {  
           this.quantity = quantity;  
      }  
      @Override  
      public int compareTo(OFruit fruit) {  
           return this.name.compareTo(fruit.getName());  
      }  
 }  



 package CollectionFramework;  
 import java.util.ArrayList;  
 import java.util.Collections;  
 import java.util.List;  
 public class ObjectSort {  
      public static void main(String[] args) {  
           // Comparable and Comparator interfaces  
           new ObjectSort().comparable();  
      }  
      public void comparable() {  
           OFruit fruit1 = new OFruit();  
           fruit1.setName("Palm");  
           fruit1.setColour("Red");  
           fruit1.setQuantity(10);  
           OFruit fruit2 = new OFruit();  
           fruit2.setName("Banana");  
           fruit2.setColour("Green");  
           fruit2.setQuantity(15);  
           OFruit fruit3 = new OFruit();  
           fruit3.setName("Orange");  
           fruit3.setColour("Yellow");  
           fruit3.setQuantity(20);  
           OFruit fruit4 = new OFruit();  
           fruit4.setName("StarFruit");  
           fruit4.setColour("Green");  
           fruit4.setQuantity(2);  
           List<OFruit> list = new ArrayList<OFruit>();  
           list.add(fruit1);  
           list.add(fruit2);  
           list.add(fruit3);  
           list.add(fruit4);  
           // Collections.shuffle(list);  
           Collections.sort(list);  
           for (int i = 0; i < list.size(); i++) {  
                System.out  
                          .println(list.get(i).getName() + '\t' + list.get(i).getColour() + '\t' + list.get(i).getQuantity());  
           }  
      }  
 }  

1 comment:

  1. It’s a nice article. As a developer this is very helpful to me & great learning about comparable interface in java . Thanks for sharing such informative post. Sharing some additional knowledge on java interface

    ReplyDelete