To my understanding, a List is an ordered collection of items. And a Set is a collection of unique items.
Now my question is, why does LinkedHashSet, which describes an ordered collection of unique items, implement the Set interface (=> unique), but not the List interface (=> ordered)?
One possible argument is that List is intended for random access datastructures, but that would be invalidated by the fact that LinkedList doesn't have "true" random access either. In fact, LinkedHashSet is backed by an internal linked list. Also the documentation for List says otherwise:
Note that these [positional index] operations may execute in time proportional to the index value for some implementations.
If it implemented a
Listyou would be able to use it as aList:This might lead to issues with duplicates which don't appear in
Setbut are allowed inList.In other words, you shouldn't declare that something is a
Listwhen it doesn't allow duplicates even if it holds the order and allows adding, getting, removing and checking the size.