Comparable Interface:
It’s an interface under the package of java.lang.comparable, which having one method int compareTo(T o). We can use this method ordering(sort logic only) the object. It will compare this object with your specified object for ordering purpose.
Key points to remember in comparable interface:
- You must write the sort logic in the same class whose objects to be sorted. So people will call this in the name of Natural Ordering.
- The class must implements Comparable interface. So that, compareTo overriding is possible. Good thing here is, Generics possible.
- Collections.sort(List) should be called for sorting.
Return type of compareTo method is int. So three possible returning values.
int compareTo(Object obj)
1. Positive – this object is greater than obj.
2. Zero – this object is equals to obj.
3. negative – this object is less than obj.
Below I have written code for explaining comparable interface with simple Fruits class.
package com.test.javahit; public class Fruits implements Comparable{ private int fruitId; private String fruitName; public Fruits(int id,String name){ this.fruitId = id; this.fruitName = name; } public int getFruitId() { return fruitId; } public void setFruitId(int fruitId) { this.fruitId = fruitId; } public String getFruitName() { return fruitName; } public void setFruitName(String fruitName) { this.fruitName = fruitName; } // Comparing logic by fruits Id @Override public int compareTo(Object obj) { Fruits fruits = (Fruits) obj; //Compare logic with Ternary operator return (this.fruitId < fruits.fruitId) ? -1 : (this.fruitId > fruits.fruitId) ? 1 : 0; //Use below If above logic is confuse /*if (this.fruitId > fruits.getFruitId()) return 1; else if (this.fruitId < fruits.getFruitId()) return -1; else return 0;*/ } }
Next below is our main method class MainTest.
package com.test.javahit; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class MainTest { /** * @param args */ public static void main(String[] args) { //Add fruit id and name for all fruits Fruits fruit1 = new Fruits(3, "Banana"); Fruits fruit2= new Fruits(1, "Orange"); Fruits fruit3 = new Fruits(2, "Apple"); Fruits fruit4 = new Fruits(4, "Cherry"); //Create a list for fruits ListlistOfFruits = new ArrayList (); listOfFruits.add(fruit1); listOfFruits.add(fruit2); listOfFruits.add(fruit3); listOfFruits.add(fruit4); System.out.println("Before using compareTo method in Fruits class"); for (int i=0; i< listOfFruits.size(); i++) { Fruits fruitName = (Fruits)listOfFruits.get(i); System.out.println("Fruit Id :" + fruitName.getFruitId() + " Fruit Name :" + fruitName.getFruitName()); } Collections.sort(listOfFruits); System.out.println("After using Collections.sort() for Fruit Id"); for (int i=0; i< listOfFruits.size();i++) { Fruits fruitSortedList = (Fruits) listOfFruits.get(i); System.out.println("Fruit Id :" + fruitSortedList.getFruitId() + " Fruit Name :" + fruitSortedList.getFruitName()); } } }
The Fruits class is implementing Comparable interface for overriding compareTo method. This method have the logic id. Please see Fruits class comapreTo method . I used ternary operator(conditional) for comparing objects. We can use simple if else condition also to compare the objects.
Once you execute MainTest class, you will find below output:
Before using compareTo method in Fruits class Fruit Id :3 Fruit Name :Banana Fruit Id :1 Fruit Name :Orange Fruit Id :2 Fruit Name :Apple Fruit Id :4 Fruit Name :Cherry After using Collections.sort() for Fruit Id Fruit Id :1 Fruit Name :Orange Fruit Id :2 Fruit Name :Apple Fruit Id :3 Fruit Name :Banana Fruit Id :4 Fruit Name :Cherry
Comparator Interface:
It’s an interface under the package of java.util.comparator, which having one method int compare(Object o1 , Object o2).
Key points to remember in comparable interface :
- Sorting logic should be placed in separate class, so that we can write sort based on different attributes of objects. It means, you can sort fruitId, fruitName etc.,
- Some other class needs to implement comparator Interface for sorting logic, here the case is FruitsIdSortingComparator.
- Collections.sort(List, Comparator) should be called for sorting.
- Return type of compare method is int. So three possible returning values.int compare(Object o1,Object o2). It will compare thwo object o1 and o2 and will return an integer.
1. positive – o1 is greater than o2
2. zero – o1 equals to o2
3. negative – o1 is less than o1
import java.util.Comparator; public class FruitsIdSortingComparator implements Comparator{ @Override public int compare(Fruits fruits1, Fruits fruits2) { Fruits fruits = (Fruits) obj; //Compare logic with Ternary operator return (fruits1.getFruitId() < fruits2.getFruitId()) ? -1 : (fruits1.getFruitId() > fruits2.getFruitId()) ? 1 : 0; }
Below I have added below code, comparator interface logic for sorting with MainTest class.
//By using comparator interface //For Fruit ID Collections.sort(listOfFruits,new FruitsIdSortingComparator()); //For Fruit Name Collections.sort(listOfFruits, new Comparator() { @Override public int compare(Fruits o1, Fruits o2) { return o1.getFruitName().compareTo(o2.getFruitName()); } }); System.out.println("\nAfter sort by Fruit Name : \n"); for (int i=0;i<listOfFruits.size();i++) { Fruits fruitname = (Fruits) listOfFruits.get(i); System.out.println(" Fruit Name : " + fruitname.getFruitName()); }
Now just run MainTest class, you able to find the below output.
After sort by Fruit Name :
Fruit Name : Apple
Fruit Name : Banana
Fruit Name : Cherry
Fruit Name : Orange
No comments:
Post a Comment