Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply 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: 24081
    
  15

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
 
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 and comparisons
Comparator
Sorting an ArrayList of Object X
Collections
Sorting in Collection Framework