Friday, 6 May 2016

HashSet vs. TreeSet


ORDER

  • HashSet stores in random order.
  • TreeSet stores in natural order.


NULL VALUES

  • HashSet allows null
  • TreeSet will throw NullPointerException if you store null object


SPEED

  • HashSet is much faster than TreeSet


PERFORMANCE

  • HashSet has constant time performance for basic operation
    (add / remove / contains / size)
  • TreeSet has log(n) time cost for basic operation


FUNCTIONALITY

  • TreeSet is more rich than HashSet - pollFirst(), pollLast(), first(), last(), ceiling(), lower() methods


No comments:

Post a Comment

Note: only a member of this blog may post a comment.