Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why Collections.sort() is there only for List implementation classes?

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody please tell me as why Collections.sort is there only for List implementation classes??

Thanks in advance.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic