• 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

why collections.sort method designed to take only list as argument and not any set?

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why collections.sort method designed to take only list as argument and not any set?

The one of the reasons I wanted to know is if there is a reason it will be easy to remember that sort method takes only list .


 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure.
May be because there is already a SortedSet which stores the elements in default sorting order.
So no need to provide same functionality again as Collections.sort(Set<E> set)
This is purely a guess.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because Set interface does not guarantee any ordering.
So there is no point forcing an order on it.

Javadoc of Set.iterator() says

Iterator<E> iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).


The unless part refers to some classes like TreeSet or LinkedHashSet that have a specfied iteration order.
 
Ags Karan
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for posting the replies.

So , As per kathy seira book An unordered collection can never be sorted . But all list are ordered by index but not sorted so it can be sorted using Collections.sort method
where as
HashSet unordered and hence no point in sorting an unordered collection
TreeSet - already sorted by natural order or comparable
LinkedHashSet - ordered by insertion order(Exceptional case where ordered but still cannot be sorted )

are my understanding of above points correct ?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ags Karan wrote:LinkedHashSet - ordered by insertion order(Exceptional case where ordered but still cannot be sorted )


You can't say LinkedHashSet can't be ordered. Since it implements Collection but not List, so it doesn't allow index based access/insertion. So it won't be easy to sort it. But since it maintains insertion order, you can always sort it. You'll have to put some extra effort, you can find the smallest element, remove it and add it at the end, then 2nd smallest and remove-then-insert it at the end, then 3rd smallest and so on...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic