Technical Articles => Java =>  Java

Comparable and comparator in Java

Source : Peter    Date : 2012-07-04 12:06:15  

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
Save as PDF Mark as read Mark as important
By clicking the "Mark as read" button, this article will be marked as read. It will be removed from the homepage's latest news and the article list on the "Technical article" page in following visits and it will be put to your read list which you can find in "Amin->Article read list". There you can unmark the read articles.
By clicking the "Mark as important" button, this article will be put to your important article list which you can find in "Amin->Article important list". Later when you want reread this article, it's easier for you to find it by checking the "Article important list".

Tags:Java,Comparable,Comparator,Sort   Read(2311) Comment(0)

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Digg  Share on Tumblr    Delicious 

 Previous : Regular expression to get html meta description
 Next : SQLite C/C++ function interfaces

  ::Related Articles

  ::Comment List

No comment for this article.

  ::Comment

Nickname  
Email 
Comment

:: Users edited this page

No users edited this page yet.

:: Recent articles

:: Most read

:: Most commented

:: Find us

Back to top