File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 24166
    
  30

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: 19543
    
  16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why Collections.sort() is there only for List implementation classes?
 
Similar Threads
Collections
Sorting in Collection Framework
Collections and comparisons
Sorting an ArrayList of Object X
Comparator