Java / Set and its implementations
Difference between HashSet and TreeSet in Java.
HashSet stores the object in random order. There is no guarantee that the element we inserted first in the HashSet will be printed first in the output . TreeSet maintains the elements in sorted order.
HashSet can store null object while TreeSet does not allow null object. If one try to store null object in TreeSet object , it will throw NullPointerException.
HashSet are internally backed by hashmap. While TreeSet is backed by a Navigable TreeMap.
HashSet take constant time performance for the basic operations like add, remove contains and size.While TreeSet guarantees log(n) time cost for the basic operations (add,remove,contains).
More Related questions...