| Author |
why Collections.sort() is there only for List implementation classes?
|
RaviNada Kiran
Ranch Hand
Joined: Jan 30, 2009
Posts: 528
|
|
Can anybody please tell me as why Collections.sort is there only for List implementation classes??
Thanks in advance.
|
If you want something you never had do something which you had never done
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
|
Because Lists have a concept of element order, while Sets and Maps do not. You can't ask a Set for its third element -- there's no "get(int)" in the Set interface. Without the idea of element order, you obviously can't sort!
|
[Jess in Action][AskingGoodQuestions]
|
 |
RaviNada Kiran
Ranch Hand
Joined: Jan 30, 2009
Posts: 528
|
|
Great explanation
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
TreeSet / TreeMap and LinkedHashSet / LinkedHashMap also have a notion of order, but that order is fixed; the entire internal structure is built for the one specific order. If the order would need to change, so would the internal structure, and you can just as well create a new object.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: why Collections.sort() is there only for List implementation classes?
|
|
|