Search code examples
javasettreesetsortedset

Why is subSet method specified in SortedSet Interface instead of Set?


Why is subSet method specified in SortedSet Interface instead of Set unlike subList method of List interface?


Solution

  • A subSet operation requires the underlying Set to have ordering. Otherwise there's no meaning to request all the elements from element X to element Y.

    The base Set interface doesn't require ordering (for example, HashSets are not ordered). Therefore it can't support this operation.

    The List interface is ordered, so having subList() method there makes sense.