It would be bad for client code that uses an interface to depend on an implementation detail that is not documented on the interface.
In your example, the Set
and Map
interfaces make no guarantees about the order of the elements inside them, so it would be bad for the client to make assumptions about them.
But this is not a problem in your case, as there are more specific sub-interfaces of Set
and Map
that do make guarantees about order. They are java.util.SortedSet
and java.util.SortedMap
. If your client code uses these interfaces, then they are allowed to make assumptions about order.
TreeSet
and TreeMap
implement these interfaces, respectively.
From the Javadoc for SortedMap
:
A
Map
that further provides a total ordering on its keys.