1. Comparison between objects in Java
Objects in Java are entities with certain pre-defined attributes. These attributes have their respective significance in their class. For instance, if there is an object of Result class, marks determine the value of object, if there is an object of TouristPlace
class, distance or pricing or both determine the value or priority of the object. Hence, when it comes to comparing objects one needs to compare the respective attributes each time.
However, this is not possible when we have a list of objects to be sorted as per a certain set of attributes. Java provides a solution to this in the form of Comparable
Interface.
2. Comparable Interface to compare Objects
Comparable
interface in Java is a way of defining the criteria of object comparison explicitly. This definition will act as the backend for comparison between objects during a sorting. A Comparable
interface defines a method compareTo(T arg0)
in which one can define the criteria to compare objects.
Let us understand this with an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.abk; public class Car implements Comparable { private String name; private double speed; private double price; public Car(String name,double price,double speed){ this.name = name; this.speed=speed; this.price = price; } @Override public int compareTo(Car c) { if(this.price > c.getPrice() && this.speed<c.getSpeed()) return 1; else if(this.price>c.getPrice() && this.speed>c.getSpeed()) return 0; else if(this.price<c.getPrice() && this.speed>c.getSpeed()) return -1; else return 0; } public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
The above class implements Comparable Interface and correspondingly implements the compareTo method to define criteria for compare 2 objects of this class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.abk; import java.util.ArrayList; import java.util.Collections; public class ComparisonTest { public static void main(String[] args) { Car c1 = new Car("Zoomer",7.99, 200); Car c2 = new Car("Booster",6.99,300); Car c3 = new Car("Small Wonder",3.99,210); ArrayList cars = new ArrayList<>(); cars.add(c1); cars.add(c2); cars.add(c3); Collections.sort(cars); for(Car c : cars){ System.out.println(c.getName()+"\nPrice: "+c.getPrice()+"\nSpeed: "+c.getSpeed()+"\n"); } } } |
On executing the above code, we get the below output
1 2 3 4 5 6 7 8 9 10 11 |
Booster Price: 6.99 Speed: 300.0 Small Wonder Price: 3.99 Speed: 210.0 Zoomer Price: 7.99 Speed: 200.0 |
The code above sorts the car objects using the compareTo
method. When the collections API is used to sort a class implementing the Comparable
interface, it implicitly uses the compareTo
method in order to derive the object priority. The compareTo
method returns either of the 3 values: 1(greater), 0(equal) or -1(lower).
Thus, the problem of comparing the objects with a set of attributes is resolved in this manner.