aspose file tools*
The moose likes Java in General and the fly likes why Collections.sort() is there only for List implementation classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why Collections.sort() is there only for List implementation classes?" Watch "why Collections.sort() is there only for List implementation classes?" New topic
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: 24183
    
  34

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: 19672
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: why Collections.sort() is there only for List implementation classes?