Friday, July 26, 2019

SortedSet Vs TreeSet In Java

Note: SortedSet is an interface and TreeSet is its implementation class.

SortedSet:

public interface SortedSet<E>
extends Set<E>

All Known Subinterfaces:

NavigableSet<E>

All Known Implementing Classes:

ConcurrentSkipListSet, TreeSet

  • SortedSet interface is a sub-interface of Set interface.
  • SortedSet present in java.util package extends the Set interface present in the collection framework.

TreeSet:

public class TreeSet<E>
extends AbstractSet<E>
implements NavigableSet<E>, Cloneable, Serializable

All Implemented Interfaces:

Serializable, Cloneable, Iterable<E>, Collection<E>, NavigableSet<E>, Set<E>, SortedSet<E>

NavigableSet:

All Superinterfaces:
Collection<E>, Iterable<E>, Set<E>, SortedSet<E>

All Known Implementing Classes:
ConcurrentSkipListSet, TreeSet

public interface NavigableSet<E>
extends SortedSet<E>

  • TreeSet class is implementations of the SortedSet interface in Java that uses a Tree to store data.
  • Elements or Objects are stored in a sorted and ascending order.
  •  Access and retrieval times are quite fast.
  • TreeSet uses a self-balancing binary search tree, more specifically a Red-Black tree. 
  • TreeSet allows only unique element/objects to be inserted
  • TreeSet object doesn't allows null values . If we try to store the null value in a TreeSet, this will thrown NullPointerException.


Related Tutorials 

No comments:

Post a Comment