Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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
  • 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
  • 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: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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...
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!