Comparable vs. Comparator
Comparable and Comparator are two interfaces provided by Java core APIs. From their names, we can roughly guess that they are used to compare some things in some way. But what are the differences between them? The following two examples answer this question. This example is used to compare the HDTV size. After reading the following code, you will be clear about how to use Comparable and Comparator.
1. Comparable
A class implements this interface to compare its own object with other objects. If a class needs to compare its own instance, this interface must be implemented. In addition, the compareTo () method must be implemented. Example:
class HDTV implements Comparable
{private int size;private String brand; public HDTV(int size, String brand) {this.size = size;this.brand = brand;} public int getSize() {return size;} public void setSize(int size) {this.size = size;} public String getBrand() {return brand;} public void setBrand(String brand) {this.brand = brand;} @Overridepublic int compareTo(HDTV tv) { if (this.getSize() > tv.getSize())return 1;else if (this.getSize() < tv.getSize())return -1;elsereturn 0;}} public class Main {public static void main(String[] args) {HDTV tv1 = new HDTV(55, "Samsung");HDTV tv2 = new HDTV(60, "Sony"); if (tv1.compareTo(tv2) > 0) {System.out.println(tv1.getBrand() + " is better.");} else {System.out.println(tv2.getBrand() + " is better.");}}
2. In some scenarios, you may not want to change a class to be comparable. For example, if you want to compare Objects Based on specific attributes/fields, you can use Comparator. For example, we can compare two people by height or age. We need to implement the compare () method. Now let's compare the TV size in another way. One of the most common applications of Comparator is sorting. Both Collections and Arrays provide methods for sorting by Comparator.
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator; class HDTV {private int size;private String brand; public HDTV(int size, String brand) {this.size = size;this.brand = brand;} public int getSize() {return size;} public void setSize(int size) {this.size = size;} public String getBrand() {return brand;} public void setBrand(String brand) {this.brand = brand;}} class SizeComparator implements Comparator
{@Overridepublic int compare(HDTV tv1, HDTV tv2) {int tv1Size = tv1.getSize();int tv2Size = tv2.getSize(); if (tv1Size > tv2Size) {return 1;} else if (tv1Size < tv2Size) {return -1;} else {return 0;}}} public class Main {public static void main(String[] args) {HDTV tv1 = new HDTV(55, "Samsung");HDTV tv2 = new HDTV(60, "Sony");HDTV tv3 = new HDTV(42, "Panasonic"); ArrayList
al = new ArrayList
();al.add(tv1);al.add(tv2);al.add(tv3); Collections.sort(al, new SizeComparator());for (HDTV a : al) {System.out.println(a.getBrand());}}}
In addition, we can use Collections. reverseOrder () to obtain a reverse-order comparator. Similar to the following:
ArrayList
al = new ArrayList
();al.add(3);al.add(1);al.add(2);System.out.println(al);Collections.sort(al);System.out.println(al); Comparator
comparator = Collections.reverseOrder();Collections.sort(al,comparator);System.out.println(al);
3. How to choose?
To put it simply, after a class implements the Comparable interface, it will become a comparison, that is, its instances can be compared with each other.
A class implements the Comparator interface. It is mainly used in two cases: 1) It can be passed to a sorting method, such as Collections. sort () or Arrays. sort (), to precisely control the sorting method. 2) It can also be used to control the sorting of specific data structures, such as TreeSet or TreeMap.
For example, to create a TreeSet, we can pass a comparator to its constructor or implement the comparable interface.
Implementation 1 (using comparator)
class Dog {int size; Dog(int s) {size = s;}} class SizeComparator implements Comparator
{@Overridepublic int compare(Dog d1, Dog d2) {return d1.size - d2.size;}} public class ImpComparable {public static void main(String[] args) {TreeSet
d = new TreeSet
(new SizeComparator()); // pass comparatord.add(new Dog(1));d.add(new Dog(2));d.add(new Dog(1));}}
Implementation 2 (using comparable)
class Dog implements Comparable
{int size; Dog(int s) {size = s;} @Overridepublic int compareTo(Dog o) {return o.size - this.size;}} public class ImpComparable {public static void main(String[] args) {TreeSet
d = new TreeSet
();d.add(new Dog(1));d.add(new Dog(2));d.add(new Dog(1));}}