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.
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
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.
posted 5 years ago
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
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 )
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...