Comparable and comparator in Java

Summary

Java offers Comparable and Comparator interfaces to define object comparison logic, which is fundamental for sorting collections. Comparable allows an object to define its natural ordering by implementing the compareTo method directly within its class, enabling instances to compare themselves. Conversely, Comparator provides an external comparison mechanism, where a separate class implements the compare method to define an ordering for other objects, useful for custom or multiple sorting criteria without altering the original class. These interfaces are key for flexible object sorting in Java applications.

Comparable and comparator are two similar interfaces used to compare objects in Java. When we want to sort a list of objects such as Employee or User etc, we may need to implement these interfaces to make them comparable as we want. However, there are some differences between comparable and comparator interface.

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.

To help you understand this, we use two examples.

Comparable
 
import java.util.Arrays; 
 
public class User implements Comparable<Object> { 
 
    private String id; 
    private int age; 
 
    public User(String id, int age) { 
        this.id = id; 
        this.age = age; 
    } 
 
    public int getAge() { 
        return age; 
    } 
 
    public void setAge(int age) { 
        this.age = age; 
    } 
 
    public String getId() { 
        return id; 
    } 
 
    public void setId(String id) { 
        this.id = id; 
    } 
 
    @Override 
    public int compareTo(Object o) { 
        return this.age - ((User) o).getAge(); 
    } 
 
    /**
     * Test method
     */ 
    public static void main(String[] args) { 
        User[] users = new User[] { new User("a", 30), new User("b", 20) }; 
        Arrays.sort(users); 
        for (int i = 0; i < users.length; i++) { 
            User user = users[i]; 
            System.out.println(user.getId() + " " + user.getAge()); 
        } 
    } 


Comparator

import java.util.Arrays; 
import java.util.Comparator; 
 
public class MyComparator implements Comparator<Object> { 
 
    @Override 
    public int compare(Object o1, Object o2) { 
        return toInt(o1) - toInt(o2); 
    } 
 
    private int toInt(Object o) { 
        String str = (String) o; 
        str = str.replaceAll("One", "1"); 
        str = str.replaceAll("Two", "2"); 
        str = str.replaceAll("Three", "3"); 
        return Integer.parseInt(str); 
    } 
 
    /**
     * Test method
     */ 
    public static void main(String[] args) { 
        String[] array = new String[] { "One", "Three", "Two" }; 
        Arrays.sort(array, new MyComparator()); 
        for (int i = 0; i < array.length; i++) { 
            System.out.println(array[i]); 
        } 
    } 
}

Reference : http://zhanghu198901.iteye.com/blog/1575164
JAVA COMPARABLE COMPARATOR SORT

  RELATED

  COMMENTS

0

No comment for this article.