VOOZH about

URL: https://www.geeksforgeeks.org/java/difference-between-treemap-and-treeset-in-java/

⇱ Difference between TreeMap and TreeSet in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference between TreeMap and TreeSet in Java

Last Updated : 23 Jul, 2025

TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order.

Some important features of the TreeSet are:

  • In TreeSet duplicate values are not allowed because it implements the interface.
  • Objects in a TreeSet are stored in ascending order.
  • In TreeSet the insertion order of elements does not maintain.

TreeMap is an implementation of Map Interface. TreeMap is also an implementation of NavigableMap along with AbstractMap class.

Some important features of the TreeMap are:

  • In TreeMap null keys(like Map) are not allowed and thus a is thrown (Multiple null values may be associated with different keys).
  • TreeMap does not support the method.

Below is the illustration of TreeSet and TreeMap in Java:

Example 1:


Output
[3, 4, 5]
{2=3, 3=5, 4=5}

Example 2:


Output
[FOR, geeks, tutorial]
{1=geeks, 2=FOR, 3=geeks, 4=tutorial}
S. No.

TreeSet

TreeMap

1.TreeSet implements SortedSet in Java.TreeMap implements Map Interface in Java
2.TreeSet stored a single object in java.TreeMap stores two Object one Key and one value.
3.TreeSet does not allow duplication Object in java.TreeMap in java allows duplication of values.
4.TreeSet implements NavigableSet in Java.TreeMap implements NavigableMap in Java.
5.TreeSet is sorted based on objects.TreeMap is sorted based on keys.
Comment