Why doesn't there exist a class in the standard library that implements both the Set
and the List
interfaces? Linked set retains the order of a set but doesn't allow random access.
Why isn't this seen as useful?
Why doesn't there exist a class in the standard library that implements both the Set
and the List
interfaces? Linked set retains the order of a set but doesn't allow random access.
Why isn't this seen as useful?
Because it's extremely hard to implement efficiently, for one, at least if you're supporting removes. For example, Guava's ImmutableSet
supports random access via its asList()
view, but doesn't support mutation.
Because it is impossible to do so without breaking the contract of Set
or List
. A Set
may not hold duplicates. What happens if you use the container as a List
and try to add a duplicate?