- 浏览: 76762 次
文章分类
- 全部博客 (89)
- web service (9)
- subversion (1)
- JBOSS (3)
- interview (23)
- jQery (2)
- ExtJs (0)
- Axis (0)
- Design pattern (3)
- Agile (2)
- mutithread (0)
- Core Java (24)
- programming methods (1)
- SSH (7)
- jee design (1)
- OO (4)
- books (8)
- other (1)
- JSF (7)
- seam (2)
- Weblogic (4)
- JPA (1)
- ADF (1)
- Spring (5)
- Tomcat (1)
- DWR (2)
- JEE (3)
- Servlet (1)
- EJB (1)
- JDBC (3)
最新评论
-
iloveflower:
呵呵。好好学习。。。。。。。。。。。。
java 读书 -
Eric.Yan:
看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
java 读书
Read more: http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html#ixzz1uaFRiHo5
What is Difference between Comparator and Comparable in Java question was asked in a Test paper for one of big Investment bank first round of interview. It was not that straight forward but it was related to how you will sort Employee object based on his EmployeeID and his name and that involves the use of both Comparable and Comparator in Java.
Comparators and comparable in Java are two of fundamental interface of Java API which is very important to understand to implement sorting in Java. It’s often required to sort objects stored in any collection class or in Array and that time we need to use compare () and compare To () method defined in java.util.Comparator and java.lang.Comparable class. Let’s see some important points about both Comparable and Comparator in Java before moving ahead
Difference between Comparator and Comparable in Java
1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package.
2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.
4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.
5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.
6)Objects which implement Comparable in Java can be used as keys in a sorted map or elements in a sorted set for example TreeSet, without specifying any Comparator.
Example of using Comparator and Comparable in Java
So in Summary if you want to sort based on natural order or object then use Comparable in Java and if you want to sort on some other attribute of object then Comparator in Java is the way to go. Now to understand these concepts lets see an example
1) There is class called Person, sort the Person based on person_id.
2) Sort the Person based on Name.
For a Person class sorting based on person_id can be treated as natural order sorting and sorting based on Name can be implemented using Comparator interface. To sort based on person_id we need to implement compareTo() method.
public class Person implements Comparable {
private int person_id;
private String name;
/**
* Compare current person with specified person
* return zero if person_id for both person is same
* return negative if current person_id is less than specified one
* return positive if specified person_id is greater than specified one
*/
public int compareTo(Person o) {
return this.person_id - o.person_id ;
}
….
}
And for sorting based on person name we can implement compare (Object o1, Object o2) method of Comparator in Java or Java Comparator class.
public class PersonSortByPerson_ID implements Comparator{
public int compare(Person o1, Person o2) {
return o1.getPersonId() - o2.getPersonId();
}
}
You can write several types of Java Comparator based upon your need for example reverseComparator , ANDComparator , ORComparator etc which will return negative or positive number based upon logical results.
What is Difference between Comparator and Comparable in Java question was asked in a Test paper for one of big Investment bank first round of interview. It was not that straight forward but it was related to how you will sort Employee object based on his EmployeeID and his name and that involves the use of both Comparable and Comparator in Java.
Comparators and comparable in Java are two of fundamental interface of Java API which is very important to understand to implement sorting in Java. It’s often required to sort objects stored in any collection class or in Array and that time we need to use compare () and compare To () method defined in java.util.Comparator and java.lang.Comparable class. Let’s see some important points about both Comparable and Comparator in Java before moving ahead
Difference between Comparator and Comparable in Java
1) Comparator in Java is defined in java.util package while Comparable interface in Java is defined in java.lang package.
2) Comparator interface in Java has method public int compare (Object o1, Object o2) which returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. While Comparable interface has method public int compareTo(Object o) which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
3) If you see then logical difference between these two is Comparator in Java compare two objects provided to him, while Comparable interface compares "this" reference with the object specified.
4) Comparable in Java is used to implement natural ordering of object. In Java API String, Date and wrapper classes implement Comparable interface.
5) If any class implement Comparable interface in Java then collection of that object either List or Array can be sorted automatically by using Collections.sort() or Arrays.sort() method and object will be sorted based on there natural order defined by CompareTo method.
6)Objects which implement Comparable in Java can be used as keys in a sorted map or elements in a sorted set for example TreeSet, without specifying any Comparator.
Example of using Comparator and Comparable in Java
So in Summary if you want to sort based on natural order or object then use Comparable in Java and if you want to sort on some other attribute of object then Comparator in Java is the way to go. Now to understand these concepts lets see an example
1) There is class called Person, sort the Person based on person_id.
2) Sort the Person based on Name.
For a Person class sorting based on person_id can be treated as natural order sorting and sorting based on Name can be implemented using Comparator interface. To sort based on person_id we need to implement compareTo() method.
public class Person implements Comparable {
private int person_id;
private String name;
/**
* Compare current person with specified person
* return zero if person_id for both person is same
* return negative if current person_id is less than specified one
* return positive if specified person_id is greater than specified one
*/
public int compareTo(Person o) {
return this.person_id - o.person_id ;
}
….
}
And for sorting based on person name we can implement compare (Object o1, Object o2) method of Comparator in Java or Java Comparator class.
public class PersonSortByPerson_ID implements Comparator{
public int compare(Person o1, Person o2) {
return o1.getPersonId() - o2.getPersonId();
}
}
You can write several types of Java Comparator based upon your need for example reverseComparator , ANDComparator , ORComparator etc which will return negative or positive number based upon logical results.
发表评论
-
Java Collection summary
2012-06-16 02:40 534Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 748I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 654How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 666Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 785Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 764http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 888A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 663各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 4751、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 786import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 538There are some important thing ... -
core java interview summary
2012-06-12 04:11 344http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 522Question: 1) Write code to che ... -
OO Design books -good website
2012-06-07 03:13 658I’m always on the search on goo ... -
Write a String Reverser (and use Recursion!)
2012-06-07 03:03 489Java Interview questions: Write ... -
Java高手必会的要点
2012-05-29 03:28 579http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1710This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1509Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 874Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 812ArrayList in Java is most frequ ...
相关推荐
教你学会Comparator和Comparable
java排序Comparator和Comparable
java的Comparator和Comparable
Java-中的-Comparator-和-Comparable.md
本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明
comparator接口与Comparable接口的区别
NULL 博文链接:https://hellogava.iteye.com/blog/1570101
Comparable与Comparator的区别Java开发Java经验技巧共3页.pdf.zip
java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子
Comparable&Comparator区别,看完就明白了
Java中Comparable和Comparator的区别共3页.pdf.zip
主要介绍了java 中Comparable与Comparator详解与比较的相关资料,需要的朋友可以参考下
通过简单的例子初步了解Comparable和Comparator的使用,注释很详细
详细的介绍了Java中Comparable与Comparator的区别
主要介绍了Java Comparable和Comparator对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Comparable和Comparator接口都可用作普通意义上对象间的比大小,但两个接口在实例化方面的用法不尽相同,接下来我们就来详细对比Java中的Comparable排序接口和Comparator比较器接口
1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Person{ String name; int age }. 当我们有这么一个...
NULL 博文链接:https://gghaomm.iteye.com/blog/1753970