Re: Why don't Lists have a Comparator?
From: Phillip Lord (p.lord_at_russet.org.uk)
Date: 04/11/04
- Next message: Peter Carter: "java.NIO + select + Network Interface Down = 100% CPU Usage"
- Previous message: Joona I Palaste: "Re: Why don't Lists have a Comparator?"
- In reply to: Joe: "Re: Why don't Lists have a Comparator?"
- Next in thread: Dale King: "Re: Why don't Lists have a Comparator?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 11 Apr 2004 16:37:10 +0100
>>>>> "Joe" == Joe <sfjoe@BLOCKEDspamcop.net> writes:
Joe> In article <c5bivo$2rccl9$1@ID-18956.news.uni-berlin.de>,
Joe> tcn@spamgourmet.org says...
>> Hi!
>>
>> Why do Lists not have an optional Comparator? I don't quite
>> understand the sense of Collections.sort() - which even does dump
>> the list to an array and sort that array...
>>
>> Timo
>>
Joe> Say you had a List of these objects:
Joe> public class MyClass {
Joe> private int someValue; private int anotherValue;
Joe> }
Joe> How would you sort those objects? If you look at the javadocs
Joe> for the Collections class, you'll read that "All elements in
Joe> the list must implement the Comparable interface". That's how
Joe> Collections.sort() works
Joe> - by calling the compareTo() method on the members.
The OP is asking why not allow for an optional comparitor object. This
way you could change the natural sorting of Objects. This means you
could sort according objects which did not implement Comparable
directly, or sort Comparable objects in a different way.
I'm not quite sure what the OP wants though. List itself does nothing
that requires Comparision. Methods which do require this (Like
Collections.sort()) normally have an overloaded method which does take
a Comparitor object. Conversely interfaces such as SortedMap do
support an external Comparitor. As does SortedSet which is probably
what he wants. Of course a Set with an order is really a list, so
perhaps this is a misnomer. But I guess they didn't want the ordered
update (add( Object, int )) methods they would have got from List.
Phil
- Next message: Peter Carter: "java.NIO + select + Network Interface Down = 100% CPU Usage"
- Previous message: Joona I Palaste: "Re: Why don't Lists have a Comparator?"
- In reply to: Joe: "Re: Why don't Lists have a Comparator?"
- Next in thread: Dale King: "Re: Why don't Lists have a Comparator?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|