Comparable and comparator both are an interface that can be used to sort the elements of the collection. Comparator interface belongs to java.util package while comparable belongs to java.lang package. Comparator interface sort collection using two objects provided to it, whereas comparable interface compares" this" refers to the one objects provided to it.
Sr. No. | Key | Comparable | Comparator |
---|---|---|---|
1 | Methods | The comparable interface has a method compareTo(Object a ) | The comparator has a method compare(Object o1, Object O2) |
2 | Sorting uses | Collection.sort(List) method can be used to sort the collection of Comparable type objects. | Collection.sort(List, Comparator) method can be used to sort the collection of Comparator type objects. |
3 | Sorting sequence | Comparable provides single sorting sequence. | The comparator provides a multiple sorting sequence. |
4 | Package | Comparable interface belongs to java.lang package. | Comparator interface belongs to java.util package. |
Example of Comparable
public class ComparableExample { public static void main(String[] args) { List<Laptop> laptopList = new ArrayList<>(); laptopList.add(new Laptop("HCL", 16, 800)); laptopList.add(new Laptop("Apple", 8, 100)); laptopList.add(new Laptop("Dell", 4, 600)); Collections.sort(laptopList); for (Laptop lap : laptopList) { System.out.println(lap.getRam()); } } } public class Laptop implements Comparable<Laptop> { String name; int ram; int price; public Laptop(String name, int ram, int price) { super(); this.name = name; this.ram = ram; this.price = price; } public String getName() { return name; } public int getRam() { return ram; } public void setRam(int ram) { this.ram = ram; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public int compareTo(Laptop o) { if (this.ram > o.getRam()) return 1; else { return -1; } } }
Output
4 8 16
Example of Comparator
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Laptop implements Comparator { String name; int ram; int price; public Laptop(String name, int ram, int price) { super(); this.name = name; this.ram = ram; this.price = price; } public String getName() { return name; } public int getRam() { return ram; } public void setRam(int ram) { this.ram = ram; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public int compare(Laptop o1, Laptop o2) { if (o1.getRam() < o2.getRam()) { return -1; }else if (o1.getRam() > o2.getRam()) { return 1; } else { return 0; } } public static void main(String[] args) { List laptopList = new ArrayList<>(); laptopList.add(new Laptop("HCL", 16, 800)); laptopList.add(new Laptop("Apple", 8, 100)); laptopList.add(new Laptop("Dell", 4, 600)); Comparator com = (Laptop o1, Laptop o2) -> o1.getName().compareTo(o2.getName()); Collections.sort(laptopList, com); for (Laptop lap : laptopList) { System.out.println(lap.getName()); } } }
Output
Apple Dell HCL