Comparable
and is the Comparator
interfaces provided by Java Core API. From their names, we can tell they is used for comparing stuff in some. But what exactly was they and what is the difference between them? The following is examples for answering this question. The simple examples compare the size of the HDTV ' s. How to use comparable vs. Comparator are obvious after reading the code.
1. Comparable
Comparable
is implemented by a class in order to being able to comparing object of itself with some other objects. The class itself must implement the interface in order to being able to compare its instance (s). The method required for implementation are compareTo()
. Here are an example:
Class HDTV implements comparable |
Sony is better.
2. Comparator
In some situations, the want to change a class and make it comparable. In such cases, Comparator
can is used if you want to compare objects based on certain attributes/fields. For example, 2 persons can is compared based on ' height ' or ' age ' etc. (this can is not being done using comparable.)
The method required to implement is compare (). Now let's use another-compare those TV by size. One common use of is Comparator
sorting. Both Collections
and Arrays
classes provide a sort method which use a 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 |
Output:
Panasonicsamsungsony
Often We may use the Collections.reverseOrder()
method to get a descending order Comparator. Like the following:
Arraylist<integer> al = new arraylist<integer> (); Al.add (3); Al.add (1); Al.add (2); System.out.println (AL); Collections.sort (AL); System.out.println (AL); comparator<integer> Comparator = Collections.reverseorder (); Collections.sort (Al,comparator); System.out.println (AL); |
Output:
[3,1,2] [A] [3,2,1]
3. When do I use Which?
In brief, a-class that implements'll be Comparable
comparable, which means it instances can is compared with each other.
A class that implements'll be Comparator
used in mainly and situations:1) It can be passed to a sort method, such as Collections.sort()
O R Arrays.sort()
, to allow precise control over the sort order and 2) It can also is used to control the order of certain data Stru Ctures, such as sorted sets (e.g. TreeSet
) or sorted maps (e.g., TreeMap
).
For example, to create a TreeSet
. We can either pass the constructor a comparator or make the object class comparable.
Approach 1-treeset (Comparator Comparator)
Class Dog {int size; Dog (int s) {size = s;}} Class Sizecomparator implements comparator<dog> {@Overridepublic int compare (dog D1, dog D2) {return d1.size-d2.si Ze;}} public class Impcomparable {public static void main (string[] args) {treeset<dog> d = new treeset<dog> (New Size Comparator ()); Pass Comparatord.add (new Dog (1));d. Add (New Dog (2));d. Add (New Dog (1));} |
Approach 2-implement Comparable
Class Dog implements Comparable<dog>{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<dog> d = new treeset<dog> ();d. Add ( New Dog (1));d. Add (New Dog (2));d. Add (New Dog (1));} |
References:
1. Comparable
2. Comparator
Comparable vs. Comparator in Java