• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting in Collection Framework  RSS feed

 
anish jain
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java provides SortedMap and SortedSet interfaces and TreeMap and TreeSet as their implementation classes which we can use to sort Tree and Set respectively.

Why is there no SortedList kind of facility provided by Java?

I know using Collections.sort(), I can sort a List but not Set or Map..why is it so?

Any reason for the above two scenarios?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because Lists are already inherently ordered, while Sets are not. Collections provides the sort() method for Lists only, because you can't sort anything if it doesn't have an order.

They could have provided an interface called OrderedSet, but really, this just mimics the List interface. They figured it was more useful to provide an interface for Sets that are always sorted.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!