When to use comparator and when we should use comparable.

Use Comparable if you want to define a default (natural) ordering behaviour of the object in question, a common practice is to use a technical or natural (database?) identifier of the object for this. Use Comparator if you want to define an external controllable ordering behaviour, this can override the default ordering behaviour.

See the example where used comparable for sorting elements in ascending order.

import java.util.ArrayList;

importjava.util.Collections;

import java.util.Comparator;

import java.util.List;

class vehicle implementsComparable<vehicle> {

     int vehNumber;

     public int getVehNumber() {

           return vehNumber;

     }

     public void setVehNumber(int vehNumber) {

           this.vehNumber = vehNumber;

     }

     @Override

     public int compareTo(vehicle o) {

           int result = 0;

           return result=this.getVehNumber() – o.getVehNumber();

     }

}

class Fruit {

public class Sorting {

     public static void main(String[] args) {

           vehicle v = new vehicle();

             v.setVehNumber(123);

             vehicle v2 = new vehicle();

             v2.setVehNumber(12);

             vehicle v1 = new vehicle();

             v1.setVehNumber(1111);

            

             List<vehicle> vehs = newArrayList<vehicle>();

             vehs.add(v);vehs.add(v2);

             vehs.add(v1);

            

             Collections.sort(vehs);

             for (int i = 0; i < vehs.size(); i++) {

               System.out.println(“” + vehs.get(i).getVehNumber());

             }

          

          

           /*Fruit f1=new Fruit(“Mango1233”,1233);

           Fruit f2=new Fruit(“Mango123”,123);

          

           List<Fruit> fs=new ArrayList<Fruit>();

           fs.add(f1); fs.add(f2);

          

           Collections.sort(fs,new FruitComparator());

           System.out.println(“”+fs.get(0).getFruitName());

*/

     }

}

Example For Comparator
Fruit.java

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

class Fruit {

String fruitName;

int fruitPrice;

            public Fruit(String fruitName, int fruitPrice) {

                        super();

                        this.fruitName = fruitName;

                        this.fruitPrice = fruitPrice;

            }

            public intgetFruitPrice() {

                        return fruitPrice;

            }

            public void setFruitPrice(int fruitPrice) {

                        this.fruitPrice = fruitPrice;

            }

            public String getFruitName() {

                        return fruitName;

            }

            public void setFruitName(String fruitName) {

                        this.fruitName = fruitName;

            }

           

}

public class Sorting {

            public static void main(String[] args) {

                                               

                        Fruit f1=new Fruit(“Mango1233”,1233);

                        Fruit f2=new Fruit(“Mango123”,123);

                       

                        List<Fruit> fs=new ArrayList<Fruit>();

                        fs.add(f1); fs.add(f2);

                       

                        Collections.sort(fs,new FruitComparator());

                         for (int i = 0; i < fs.size(); i++) {                  
           System.out.println(“” + fs.get(i).getFruitPrice());

                           }

            }

}

FruitComparator.java-
This is where we will write logic for comparing two custom objects.
import java.util.Comparator;

import java.util.Comparator;

public class FruitComparator implementsComparator<Fruit> {

            public intcompare(Fruit o1, Fruit o2) {

                        int result = 0;

                        if (o1.getFruitPrice() < o2.getFruitPrice()) {

                                    result = -1;

                        } else {

                                    result = 1;

                        }

                        return result;

            }

}

Sorting.java
public class Sorting {

     public static void main(String[] args) {

                    

           Fruit f1=new Fruit(“Mango1233”,1233);

           Fruit f2=new Fruit(“Mango123”,123);

          

           List<Fruit> fs=new ArrayList<Fruit>();

           fs.add(f1); fs.add(f2);

          

           Collections.sort(fs,new FruitComparator());

            for (int i = 0; i < fs.size(); i++)                                        {                   System.out.println(“” + fs.get(i).getFruitPrice());

                  }

     }

}

Leave a Reply

Your email address will not be published. Required fields are marked *