Help the world stop coronavirus! Stay home!

Prev Next

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).

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

Show more question and Answers...

Queue and its implementations

Comments & Discussions