Comparable and comparator in Java
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
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
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".
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)
Previous : Regular expression to get html meta description Next : SQLite C/C++ function interfaces
::Related Articles
::Comment List
No comment for this article.
::Comment
:: Users edited this page
No users edited this page yet.
:: Recent articles
- Content based HTTP Cache
- Select top 3 values from each group in a table with SQL
- Meta tag in HTML header
- Text editor vs IDE
- Sorry, I don't want to download your fucking app
- Display GIF animation while submitting the web form
- PHP to get access token for Sina Weibo app
- Tencent released Q1 earning report of 2013
- How to be jQuery-free?
- Programmer's Mother's Day
- more>>
:: Most read
- TIOBE : C overtakes Java as the No.1 programming language
- Which programming language should I learn first?
- Sony is to release PlayStation4 in 2015
- Disposable Email address
- 5 Free Open Source Chat Applications For Developers
- Never ever touch a programmer
- Hacking Vs. Programming
- TIOBE : Where is that next big programming language?
- Multitasking vs multiprogramming
- Google is developing advanced programming technology to simplify Web application development
- more>>
:: Most commented
- Sony is to release PlayStation4 in 2015
- Hacking Vs. Programming
- Which programming language should I learn first?
- Error handling style in C
- 10 controversial programming opinions?
- C vs Java Complete Comparison
- Google+ is sick
- Unix Philosophy
- Disposable Email address
- Will Google+ be a "Ghost City"
- more>>
:: Find us
