Comparable and Comparator
- Sorting of objects based on natural order uses Comparable whereas if you sorting needs to be done on attributes of different objects, then use Comparator in Java
Comparable
- public class Car implements Comparable<Car>
- {
- private String maker;
- private String color;
-
- public Car(String maker, String color)
- {
- this.maker = maker;
- this.color = color;
- }
-
- public int compareTo(Car c)
- {
- return maker.compareTo(c.maker);
- }
-
- public static void main(String args[])
- {
- Car c = new Car("Buick", "White");
- Car c2 = new Car("Honda", "Blue");
-
- System.out.println(c.compareTo(c2));
-
- if(c.compareTo(c2) < 0)
- System.out.println("Less than ...");
- }
- }
-
- import java.util.*;
-
- public class Car implements Comparable<Car>
- {
- private String maker;
- private String color;
-
- public Car(String maker, String color)
- {
- this.maker = maker;
- this.color = color;
- }
-
- public int compareTo(Car c)
- {
- //System.out.println(maker+" - "+c.maker+" "+maker.compareTo(c.maker));
- return maker.compareTo(c.maker);
- }
-
- public String toString()
- {
- return maker+": "+color;
- }
-
- public static void main(String args[])
- {
- PriorityQueue<Car> q = new PriorityQueue<Car>();
-
- q.add(new Car("Buick", "White"));
- q.add(new Car("Honda", "Blue"));
- q.add(new Car("Accord", "Blue"));
- q.add(new Car("Lincoln", "White"));
-
- System.out.println(q);
-
- //poll objects in the queue by priority
- for(int i = 0, len = q.size(); i < len; i++)
- System.out.println(q.poll());
- }
- }
-
- import java.util.*;
-
- public class Car implements Comparable<Car>
- {
- private String maker;
- private String color;
-
- public Car(String maker, String color)
- {
- this.maker = maker;
- this.color = color;
- }
-
- public int compareTo(Car c)
- {
- //System.out.println(maker+" - "+c.maker+" "+maker.compareTo(c.maker));
- return maker.compareTo(c.maker);
- }
-
- public String toString()
- {
- return maker+": "+color;
- }
-
- public static void main(String args[])
- {
- SortedSet<Car> s = new TreeSet<Car>();
-
- s.add(new Car("Buick", "White"));
- s.add(new Car("Honda", "Blue"));
- s.add(new Car("Accord", "Blue"));
- s.add(new Car("Lincoln", "White"));
-
- System.out.println(s);
- }
- }
-
Comparator
- import java.util.*;
-
- public class Car
- {
- private String maker;
- private String color;
-
- public Car(String maker, String color)
- {
- this.maker = maker;
- this.color = color;
- }
-
- public String toString()
- {
- return maker+": "+color;
- }
-
- public String getMaker()
- {
- return maker;
- }
-
- public String getColor()
- {
- return color;
- }
- }
-
- import java.util.*;
-
- public class CarComparator implements Comparator
- {
- public int compare(Car c1, Car c2)
- {
- return (c1.getMaker().compareTo(c2.getMaker()));
- }
- }
-
- import java.util.*;
-
- public class Main
- {
- public static void main(String args[])
- {
- ArrayList<Car> l = new ArrayList<Car>();
-
- //add
- l.add(new Car("Buick", "White"));
- l.add(new Car("Honda", "Blue"));
- l.add(new Car("Lincoln", "White"));
- l.add(new Car("Accord", "Blue"));
-
- System.out.println(l);
-
- //sort by comparator
- Collections.sort(l, new CarComparator());
-
- System.out.println(l);
- }
- }
-
Reference