HashSet vs LinkedHashSet

December 17th, 2011 Leave a comment Go to comments

What the single difference I mentioned in java collection doodle chart is that iteration order in LinkedHashSet equals to insertion order. If you are interested to know how exactly it works then read this summary.

 

A hash table with chained overflow

A hash table with chained overflow

linked hash table

A linked hash table

A hash table with chained overflow

 

A linked hash table

 

In Hash table hash code is calculated from the element itself and then they are positioned. Since more than one element can have same hashcode so the colliding elements can have same position or bucket. Elements in same bucket are stored in linked list.

In LinkedHashSet, all the elements has extra reference to its next elements. There are 2 linked lists are maintained. One for all the elements of same bucket, another for all the elements stored.

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

  1. No comments yet.