Monday, 24 November 2014

Comparable in java

Comparable interface:

Class whose objects to be sorted must implement this interface.In this,we have to implement compareTo(Object) method.
For example:
view plainprint?
  1. public class Country implements Comparable<Country>{
  2.        @Override
  3.     public int compareTo(Country country) {
  4.         return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;
  5. }}
If any class implements comparable inteface then collection of that object can be sorted automatically using Collection.sort() or Arrays.sort().Object will be sort on the basis of compareTo method in that class.
Objects which implement Comparable in java can be used as keys in a SortedMap like TreeMap or SortedSet like TreeSet without implementing any other interface.
Sorting logic must be in same class whose objects are being sorted. Hence this is called natural ordering of objects

Java code:

For Comparable:

We will create class country having attribute id and name.This class will implement Comparable interface and implement CompareTo method to sort collection of country object by id.
1. Country.java
view plainprint?
  1. package org.arpit.java2blog;
  2. //If this.cuntryId < country.countryId:then compare method will return -1
  3. //If this.countryId > country.countryId:then compare method will return 1
  4. //If this.countryId==country.countryId:then compare method will return 0
  5. public class Country implements Comparable<Country>{
  6.     int countryId;
  7.     String countryName;
  8.     public Country(int countryId, String countryName) {
  9.         super();
  10.         this.countryId = countryId;
  11.         this.countryName = countryName;
  12.     }
  13.     @Override
  14.     public int compareTo(Country country) {
  15.        return (this.countryId < country.countryId ) ? -1: (this.countryId > country.countryId ) ? 1:0 ;
  16.     }
  17.     public int getCountryId() {
  18.         return countryId;
  19.     }
  20.     public void setCountryId(int countryId) {
  21.         this.countryId = countryId;
  22.     }
  23.     public String getCountryName() {
  24.         return countryName;
  25.     }
  26.     public void setCountryName(String countryName) {
  27.         this.countryName = countryName;
  28.     }
  29. }
2.ComparableMain.java
view plainprint?
  1. package org.arpit.java2blog;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. public class ComparableMain {
  6.  /**
  7.   * @author Arpit Mandliya
  8.   */
  9.  public static void main(String[] args) {
  10.    Country indiaCountry=new Country(1“India”);
  11.    Country chinaCountry=new Country(4“China”);
  12.    Country nepalCountry=new Country(3“Nepal”);
  13.    Country bhutanCountry=new Country(2“Bhutan”);
  14.          List<Country> listOfCountries = new ArrayList<Country>();
  15.          listOfCountries.add(indiaCountry);
  16.          listOfCountries.add(chinaCountry);
  17.          listOfCountries.add(nepalCountry);
  18.          listOfCountries.add(bhutanCountry);
  19.          System.out.println(“Before Sort  : “);
  20.          for (int i = 0; i < listOfCountries.size(); i++) {
  21.     Country country=(Country) listOfCountries.get(i);
  22.     System.out.println(“Country Id: “+country.getCountryId()+“||”+“Country name: “+country.getCountryName());
  23.    }
  24.          Collections.sort(listOfCountries);
  25.          System.out.println(“After Sort  : “);
  26.          for (int i = 0; i < listOfCountries.size(); i++) {
  27.     Country country=(Country) listOfCountries.get(i);
  28.     System.out.println(“Country Id: “+country.getCountryId()+“|| “+“Country name: “+country.getCountryName());
  29.    }
  30.  }
  31. }
Output:
view plainprint?
  1. Before Sort  :
  2. Country Id: 1||Country name: India
  3. Country Id: 4||Country name: China
  4. Country Id: 3||Country name: Nepal
  5. Country Id: 2||Country name: Bhutan
  6. After Sort  :
  7. Country Id: 1|| Country name: India
  8. Country Id: 2|| Country name: Bhutan
  9. Country Id: 3|| Country name: Nepal
  10. Country Id: 4|| Country name: China

No comments:

Post a Comment