Set Types

TreeSet And LinkedHashSet:

1 ) TreeSet in Java

TreeSet provides an implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like simple set with the exception that it stores elements in sorted format.Following are the features of TreeSet.

  • TreeSet uses tree data structure for storage.

  • Objects are stored in sorted, ascending order. But we can iterate in descending order using method TreeSet.descendingIterator().

  • Access and retrieval times are very fast which make TreeSet an excellent choice for storage of large volume of data in sorted format.

  • TreeSet doesn’t use hashCode() and equals() methods to compare it’s elements. It uses compare() (or compareTo()) method to determine the equality of two elements.

Important Methods of treeset class:

  • boolean add(E e): This method adds the specified element to this set if it is not already present.

  • E ceiling(E e): This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.

  • boolean contains(Object o): This method returns true if this set contains the specified element.

  • E floor(E e): This method Returns the greatest element in this set less than or equal to the given element, or null if there is no such element.

  • E pollFirst(): This method retrieves and removes the first (lowest) element, or returns null if this set is empty.

  • E pollLast(): This method retrieves and removes the last (highest) element, or returns null if this set is empty.

  • boolean remove(Object o):This method removes the specified element from this set if it is present.

The following is a very simple TreeSet implementation including TreeSet is sorting,iteration in a TreeSet, retrieving first and last element and remove an element.

Example :

Output :

2) LinkedHashSet in Java with Examples

A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. When cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.

Syntax:

  • Contains unique elements only like HashSet. It extends HashSet class and implements Set interface.

  • Maintains insertion order.

Differences Between HashSet, LinkedHashSet and TreeSet:

FEATURES

HASHSET

LINKEDHASHSET

TREESET

Internal Working

HashSet internally uses HashMap for storing objects

LinkedHashSet uses LinkedHashMap internally to store objects

TreeSet uses TreeMap internally to store objects

When To Use

If you don’t want to maintain insertion order but want store unique objects

If you want to maintain insertion order of elements then you can use LinkedHashSet

If you want to sort the elements according to some Comparator then use TreeSet

Order

HashSet does not maintain insertion order

LinkedHashSet maintains insertion order of objects

While TreeSet orders of the elements according to supplied Comparator. Default, It’s objects will be placed in their natural ascending order.

Complexity of Operations

HashSet gives O(1) complicity for insertion, removing and retrieving objects

LinkedHashSet gives insertion, removing and retrieving operations performance in order O(1).

While TreeSet gives performance of order O(log(n)) for insertion, removing and retrieving operations.

Performance

HashSet performance is better according to LinkedHashSet and TreeSet.

The performance of LinkedHashSet is slow to TreeSet. The performance LinkedHashSet is almost similar to HashSet but slower because, LinkedHashSet maintains LinkedList internally to maintain the insertion order of elements

TreeSet performance is better to LinkedHashSet excluding insertion and removal operations because, it has to sort the elements after each insertion and removal operations.

Compare

HashSet uses equals() and hashCode() methods to compare the objects

LinkedHashSet uses equals() and hashCode() methods to compare it’s objects

TreeSet uses compare() and compareTo() methods to compare the objects

Null Elements

HashSet allows only one null objects

LinkedHashSet allows only one null objects.

TreeSet not allow a any null objects. If you insert null objects into TreeSet, it throws NullPointerException

Syntax

HashSet obj = new HashSet();

LinkedHashSet obj = new LinkedHashSet();

TreeSet obj = new TreeSet();

Last updated

Was this helpful?