Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java significance of hashing linkedHashSet

I know following things about linkedHashSet

  • it maintains insertion order
  • uses LinkedList to preserve order
  • my question is how does hashing come into picture ??

I understand If hashing is used then the concept of bucketing comes in

However, from checking the code in the JDK it seems that LinkedHashSet implementation contains only constructor and no implementation, so I guess all the logic happens in HashSet?

  • so hashSet uses LinkedList by default ?

Let me put my question this way ... if objective is to write a collection that

  1. maintains unique values
  2. preserves insertion order using a linked list THEN ... it can easily be done without Hashing ... may be we can call this collection LinkedSet

saw a similar question what's the difference between HashSet and LinkedHashSet but not very helpful

Let me know if i need to explain my question more

like image 787
Lav Avatar asked Feb 01 '13 04:02

Lav


1 Answers

False. The implementation of LinkedHashSet is really all in LinkedHashMap. (And the implementation of HashSet is really all in HashMap. Le gasp!)

HashSet has no linked list at all.

It's entirely possible to write a LinkedSet collection backed by a linked list, that keeps elements unique -- it's just that its performance will be pretty crappy.

like image 68
Louis Wasserman Avatar answered Sep 28 '22 09:09

Louis Wasserman