javahashsetlinkedhashset

What's the implementation difference between HashSet and LinkedHashSet?


I saw that LinkedHashSet extends HashSet and I know it preserves order.
However, from checking the code in the JDK it seems that LinkedHashSet contains only constuctor and no implementation, so I guess all the logic happens in HashSet?
If that is correct, why is it implemented like that? It seems very confusing.

EDIT: There was an unfortunate mistake in the question. I wrote HashMap and LinkedHashMap instead of HashSet and LinkedHashSet.


Solution

  • Yes, LinkedHashMap calls its super constructor. One thing it does is to override the init() method, which is called by the super constructor.

    The LinkedHashMap is an HashMap with a doubly-linked list implementation added.