- 浏览: 80716 次
文章分类
- 全部博客 (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 577Collection:List、Set Map: ... -
When to use Comparable vs Comparator
2012-06-15 00:52 792I have a list of objects I need ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 696How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 720Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 826Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 802http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 939A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 705各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 5201、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 853import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 587There are some important thing ... -
core java interview summary
2012-06-12 04:11 385http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 555Question: 1) Write code to che ... -
OO Design books -good website
2012-06-07 03:13 700I’m always on the search on goo ... -
Write a String Reverser (and use Recursion!)
2012-06-07 03:03 524Java Interview questions: Write ... -
Java高手必会的要点
2012-05-29 03:28 612http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1756This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1542Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 910Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 881ArrayList in Java is most frequ ...
相关推荐
教你学会Comparator和Comparable
在Java编程语言中,排序是数据处理中一个非常常见的需求,而`Comparator`和`Comparable`接口则是实现排序的关键工具。这两个接口都是用于比较对象,但它们的应用场景和使用方式有所不同。 首先,`Comparable`接口是...
在 Java 中,Comparator 和 Comparable 是两个非常重要的接口,它们用于对象的排序操作。这两个接口都是在 java.util 包中定义的,主要用于定制排序规则。 **一、Comparator 接口** Comparator 接口允许我们创建...
Java-中的-Comparator-和-Comparable.md
Comparator接口与Comparable接口是Java语言中两个重要的接口,它们都是用于比较和排序自定义类的对象的大小的。虽然它们两个都是用于比较的接口,但是它们有着不同的实现方式和应用场景。 相同之处:Comparator接口...
在Java编程语言中,Comparable和Comparator接口是两个重要的概念,它们都用于对象的排序,但有着不同的使用场景和特点。本文将深入探讨这两个接口的区别,以及它们在实际开发中的应用。 首先,我们来了解一下...
在Java编程中,为了对自定义对象进行排序,Java提供了两种接口:`Comparable`与`Comparator`。这两种接口各有优势,适用于不同的场景。本文将深入探讨这两种接口的区别及其应用场景,帮助读者更好地理解它们的工作...
在Java编程语言中,`Comparable`接口和`Comparator`接口是两种重要的排序机制,它们用于对集合中的对象进行比较和排序。理解它们的区别对于任何Java开发者来说都是至关重要的。 首先,`Comparable`接口位于`java....
Comparable 接口和 Comparator 使用示例 在 Java 编程语言中,比较和排序是非常...Comparable 接口和 Comparator 都是 Java 中非常重要的比较和排序机制,它们提供了不同的比较和排序方式,分别适用于不同的应用场景。
在Java编程语言中,Comparable和Comparator接口是两个非常重要的组件,它们主要用来进行对象的比较和排序。了解它们之间的区别对于提升代码的可维护性和灵活性至关重要。 Comparable接口来源于java.lang包,它定义...
在Java编程语言中,排序是常见的操作,而`Comparable`和`Comparator`接口则是实现对象排序的两种主要方式。这篇文章将深入探讨这两种实现方式及其在实际编程中的应用。 首先,`Comparable`接口位于`java.lang`包下...
Java中的Comparator接口是排序的关键工具,它允许程序员自定义对象的比较规则,从而在集合、数组或树结构(如TreeSet、TreeMap)中实现定制化的排序。在Java的Collections框架中,Comparator扮演着重要的角色,特别...
在Java编程语言中,Comparable和Comparator接口用于比较对象的顺序,尤其在进行排序操作时非常关键。两者虽然目的相似,但使用方式和应用场景有所区别。 Comparable接口定义在`java.lang`包中,它只有一个方法`...
Java Comparable和Comparator对比详解 Java 中的 Comparable 和 Comparator 是两个常用的接口,用于实现对象的比较和排序。它们都是 Java 中的接口,都是用于比较对象的大小的,但它们有所不同。 Comparable 接口 ...
在Java编程语言中,Comparable和Comparator是两个非常重要的接口,它们都用于对象的比较和排序。下面是关于这两个接口的详细解释: **Comparable接口** Comparable接口位于`java.lang`包下,它是一个对象自比较的...
【Comparable和Comparator区分详解】 Comparable接口是Java中用于对象排序的关键接口,主要应用于需要自动排序的场景。当一个类实现了Comparable接口,意味着该类的对象具备内在的比较逻辑,可以直接进行比较。例如...
`Comparator`可以用于任何实现了`Comparable`接口的类,或者当我们想要对不支持自然排序的对象进行排序时。标题提到的`MyComparator`就是一个自定义的比较器类,它实现了`Comparator`接口,用于特定的算法排序。 `...