• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting in Collection Framework

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic