1

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 designed 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. I fixed the question answer it if possible.
Also, I was interested why Java designers chose to implement it like that.

oshai
  • 14,865
  • 26
  • 84
  • 140
  • 4
    Where did you see that it contains only a Constructor? There are a bunch of methods in that class, some overriding the ones of HashMap. See here for example: http://kickjava.com/src/java/util/LinkedHashMap.java.htm – Guillaume Polet May 21 '12 at 07:54
  • Please recheck the [code](http://www.docjar.com/html/api/java/util/LinkedHashMap.java.html) – jmj May 21 '12 at 07:56
  • 1
    Duplicate of http://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-sortedmap-in-java – Bhavik Ambani May 21 '12 at 08:06
  • I edited the question, I am looking for new answers regarding `LinkedHashSet` and `HashSet` – oshai Jun 12 '12 at 12:44

2 Answers2

1

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.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • I edited the question, I am looking for new answers regarding `LinkedHashSet` and `HashSet` – oshai Jun 12 '12 at 12:44
0

As you said the difference between the two data structures is that the LinkedHashMap is an HashMap that preserve the insertion order of pairs.

So the Linked one is intended to used as an HashMap via standard methods of the HashMap and the only method added is removeEldestEntry(), useful if you want to deal with the "list" part of the data structure.

Dario
  • 5,203
  • 1
  • 23
  • 26
  • I edited the question, I am looking for new answers regarding `LinkedHashSet` and `HashSet` – oshai Jun 12 '12 at 12:44