Linkedin Interview Question
Software Engineer / DevelopersTreeSet
- store set in a tree, sorted order of elements (increasing/decreasing) is preserved.
- insertion/removal/searching are O(log n)
HashSet
- store set in a hashmap, order of elements (whether sorted or insertion order) is lost
- insertion/removal/searching are O(1)
LinkedHashSet
- stores set in hashmap, plus a linked list to keep track of order of insertion
- insertion/removal/searching are O(1) , though extra space needed for the linked list
- order of insertion is preserved (due to linked list storing elements in order they are inerted)
1. TreeSet guarantees arrangement of elements according to their natural ordering always, HashSet doesn't guarantee.
- taojfew August 25, 20112. TreeSet implements Red-Black Trees(spl Binary Tree, no arrays), while HashSet uses arrays and hashing mechanism.
3. For put, get, contains(), remove: HashSet - O(c), TreeSet - O(log n).