Tuesday, April 19, 2016

TreeSet over HashSet

When to go for TreeSet over HashSet
1. Sorted unique elements are required instead of unique elements.The sorted list given by TreeSet is
   always in ascending order.

2. TreeSet has greater locality than HashSet. If two entries  are near by in the  order, then TreeSet 
   places them near each  other in  data structure and  hence in memory,  while HashSet  spreads  the 
   entries all over memory  regardless of the keys they are associated to. 
   As we know  Data reads  from the hard drive  takes  much more latency time than data read from the 
   cache or memory.In case data needs to be read from hard drive than prefer TreeSet as it has greater
   locality than HashSet.

3. TreeSet uses Red- Black tree algorithm underneath to sort out the elements. When one need to perform
   read/write operations frequently, then TreeSet is a better option.

No comments:

Post a Comment