Sunday 26 March 2017

LinkedHashSet in java

LinkedHashSet implements Set interface and extends HashSet class. It is part of java.util package.

Key feature of LinkedHashSet:

1.    LinkedHashSet is almost similar to HashSet except that it maintains the insertion order. If one use LinkedHashMap then you will get element returned in same order in which they were inserted.
To maintain insertion order, LinkedHashSet maintains a doubly-linked list running through all its entries.
2.    LinkedHashSet is non-Synchronized.
In order to Synchronize it, use Collections.synchronizedSet()
3.    Iterator returned by LinkedHashSet are fail-fast*.

*Fail-fast – If a set is modified at any time after the creation of iterator, except iterator’s own remove method, the iterator will throw ConcurrentModificationException.

Difference

·         HashSet vs LinkedHashSet
·         LinkedHashSet vs TreeSet

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.