Today's Question:  What are bad parts of Windows 10?       GIVE A SHOUT

Technical Article => Programming =>  Java

Comparable and comparator in Java

 Peter     2012-07-04 12:06:15      3,620    0    0

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

 Tags : Java,Comparable,Comparator,Sort   Read(3,620) Comment(0) Version(0)

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

 RELATED


 COMMENT  (TOTAL 0 COMMENT)


No comment for this article.


Nickname  
Email 
Comment

Ask ads

By sonic0002
When I search something on Ask.com, the first page which I can see is full of ads. Is this Ask.com or is it Ask.ads?

 CONTRIBUTE


Share your geeky stuff now.
You make the world better, the world makes us better.

 WRITE ARTICLE

 VERSION


No other version available yet.