- 浏览: 79745 次
文章分类
- 全部博客 (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 读书
I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works. Now looking back at this, I am wondering if I should have instead have the my class implement Comparable instead of creating a new class that implements Comparator. The score is the only field that the objects will be ordered on. 1. What I have done acceptable as a practice? 2. Is the right approach "First have the class implement Comparable (for the natural ordering) and if an alternative field comparison is required, then create a new class that implements Comparator" ? 3. If (2) above is true, then does it mean that one should implement Comparator only after they have the class implement Comparable? (Assuming I own the original class). Thanks! Use Comparable if you want to define a default (natural) ordering behaviour of the object in question, a common practice is to use a technical or natural (database?) identifier of the object for this. Use Comparator if you want to define an external controllable ordering behaviour, this can override the default ordering behaviour. I would say that an object should implement Comparable if that is the clear natural way to sort the class, and anyone would need to sort the class would generally want to do it that way. If, however, the sorting was an unusual use of the class, or the sorting only makes sense for a specific use case, then a Comparator is a better option. Put another way, given the class name, is it clear how a comparable would sort, or do you have to resort to reading the javadoc? If it is the latter, odds are every future sorting use case would require a comparator, at which point the implementation of comparable may slow down users of the class, not speed them up.
发表评论
-
Java Collection summary
2012-06-16 02:40 565Collection:List、Set Map: ... -
Arrays.fill with multidimensional array in Java
2012-06-15 00:09 684How can I fill a multidimension ... -
Immutable objects
2012-06-14 23:49 707Immutable objects are simply ... -
Implementing hashCode; Transaction.java
2012-06-14 23:43 814Below is the syntax highlight ... -
Lazy initialization
2012-06-14 22:48 797http://www.javapractices.com/to ... -
How to sort an array,mid of linkedlist, reverse int
2012-06-13 07:47 931A common mistake for a beginner ... -
Java各类型转换
2012-06-13 05:25 690各种数字类型转换成字符串型: String s = Str ... -
regular expression
2012-06-13 03:08 5091、Java对反斜线处理的 ... -
string functions
2012-06-13 00:09 840import java.util.*; public c ... -
String array to arraylist
2012-06-13 00:07 573There are some important thing ... -
core java interview summary
2012-06-12 04:11 377http://blog.sina.com.cn/s/blog_ ... -
programming with String
2012-06-12 01:43 549Question: 1) Write code to che ... -
OO Design books -good website
2012-06-07 03:13 691I’m always on the search on goo ... -
Write a String Reverser (and use Recursion!)
2012-06-07 03:03 519Java Interview questions: Write ... -
Java高手必会的要点
2012-05-29 03:28 604http://developer.51cto.com/art/ ... -
How to use getClass().getClassLoader().getResource()
2012-05-29 03:13 1742This is the simplest wat to get ... -
How to override equals method in Java
2012-05-12 02:57 1531Object class holds some very in ... -
Top 30 Programming interview questions
2012-05-12 02:48 901Programming questions are integ ... -
10 example of using ArrayList in Java >>> Java ArrayList Tutorial
2012-05-12 02:37 867ArrayList in Java is most frequ ... -
How to use Comparator and Comparable in Java? With example
2012-05-12 02:21 757Read more: http://javarevisited ...
相关推荐
### Comparable与Comparator的区别详解 #### 一、引言 在Java编程中,为了对自定义对象进行排序,Java提供了两种接口:`Comparable`与`Comparator`。这两种接口各有优势,适用于不同的场景。本文将深入探讨这两种...
在Java编程语言中,Comparable和Comparator接口是两个重要的概念,它们都用于对象的排序,但有着不同的使用场景和特点。本文将深入探讨这两个接口的区别,以及它们在实际开发中的应用。 首先,我们来了解一下...
在Java编程语言中,Comparable和Comparator接口是两个非常重要的组件,它们主要用来进行对象的比较和排序。了解它们之间的区别对于提升代码的可维护性和灵活性至关重要。 Comparable接口来源于java.lang包,它定义...
在Java编程语言中,Comparable和Comparator是两个非常重要的接口,它们都用于对象的比较和排序。下面是关于这两个接口的详细解释: **Comparable接口** Comparable接口位于`java.lang`包下,它是一个对象自比较的...
在Java编程语言中,排序是常见的操作,而`Comparable`和`Comparator`接口则是实现对象排序的两种主要方式。这篇文章将深入探讨这两种实现方式及其在实际编程中的应用。 首先,`Comparable`接口位于`java.lang`包下...
【Comparable和Comparator区分详解】 Comparable接口是Java中用于对象排序的关键接口,主要应用于需要自动排序的场景。当一个类实现了Comparable接口,意味着该类的对象具备内在的比较逻辑,可以直接进行比较。例如...
Comparable 接口和 Comparator 使用示例 在 Java 编程语言中,比较和排序是非常常见的操作。Comparable 接口和 Comparator 是两种常用的比较和排序方式。在本文中,我们将通过实例代码,详细介绍 Comparable 接口和...
1. **自然顺序 vs 自定义顺序**:`Comparable`提供了对象的自然顺序,而`Comparator`允许你在运行时定义自定义的比较逻辑。 2. **实现方式**:`Comparable`是通过类实现接口,而`Comparator`是在排序时创建和传递一...
Java 中的 Comparable 和 Comparator 接口详解 Java 中的 Comparable 和 Comparator 接口都是用于比较和排序的,但是它们之间存在着一些关键的区别。Comparable 是一个排序接口,若一个类实现了 Comparable 接口,...
在Java编程语言中,排序是数据处理中一个非常常见的需求,而`Comparator`和`Comparable`接口则是实现排序的关键工具。这两个接口都是用于比较对象,但它们的应用场景和使用方式有所不同。 首先,`Comparable`接口是...
Java Comparable和Comparator对比详解 Java 中的 Comparable 和 Comparator 是两个常用的接口,用于实现对象的比较和排序。它们都是 Java 中的接口,都是用于比较对象的大小的,但它们有所不同。 Comparable 接口 ...
在Java编程语言中,Comparable和Comparator接口用于比较对象的顺序,尤其在进行排序操作时非常关键。两者虽然目的相似,但使用方式和应用场景有所区别。 Comparable接口定义在`java.lang`包中,它只有一个方法`...
要注意的是List,Set,Queue继承了Collection接口,...这里想用一个简单的例子展示一下他们的使用,内容包括:List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类,泛型、重写equals、hashCode
Comparator接口与Comparable接口的区别 Comparator接口与Comparable接口是Java语言中两个重要的接口,它们都是用于比较和排序自定义类的对象的大小的。虽然它们两个都是用于比较的接口,但是它们有着不同的实现方式...
教你学会Comparator和Comparable
NULL 博文链接:https://chaoyi.iteye.com/blog/2082723
Java 比较接口comparable与comparator区别解析 Java 语言中提供了两个比较接口: Comparable 和 Comparator,这两个接口都是用于比较对象的大小顺序的,但它们有着不同的使用场景和实现方式。在本文中,我们将详细...
Java-中的-Comparator-和-Comparable.md