4 Difference between TreeMap and TreeSet in Java with Example

I have shared a lot of Java Collections Framework tutorial on this blog. Adding to this list is yet another tutorial which is difference between TreeMap and TreeSet in java with examples. This question is similar to the question difference between Map and Set interfaces with some minor tweaks. In this post we will learn about the differences, similarities and examples of TreeMap and TreeSet.

Read Also :  Difference between TreeMap and HashMap in Java

Difference between TreeMap and TreeSet in Java

Interface : TreeMap implements Map interface while TreeSet implements Set interface.

Duplicates : TreeMap allows duplicate values while TreeSet does not allow duplicate objects.

Implementation : TreeMap is a red-black tree based NavigableMap implementation.
TreeSet is a NavigableSet implementation based on a TreeMap. 

Sorting : TreeMap is sorted based on keys while TreeSet is sorted based on objects.

Similarities between TreeMap and TreeSet in Java

1. Null values : Both TreeMap and TreeSet do not permit null values.

2. Sorting : Both TreeMap and TreeSet are sorted. Sorted order can be natural sorted order defined by Comparable interface or custom sorted order defined by Comparator interface.

3. Performance : Both TreeMap and TreeSet provides guaranteed log(n) time cost for operation like get, put, containsKey and remove. Both internally uses Red-Black tree.

4. Synchronization : Both TreeMap and TreeSet are not synchronized. Hence, they are not used in concurrent applications.

5. Fail-fast Iterator : The iterator returned by the iterator method of the TreeMap and TreeSet are fail-fast. You can find more about fail-fast and fail-safe iterator here.

6. Package : Both classes belong to the java.util package and are part of the Java Collections Framework.

Example of TreeMap and TreeSet


import java.util.*;

public class TreeMapTreeSetExample {
    public static void main(String args[]) {
        TreeMap<String,String> map = new TreeMap<String,String>();
        map.put("Alive is ","Awesome");
        map.put("Love","yourself");
        map.put("Be in ","present");
        System.out.println("TreeMap object output :"+ map);        
        
        TreeSet set = new TreeSet();
        set.add("Alive is Awesome");
        set.add("Love yourself");
        set.add("Be in present");
        System.out.println("TreeSet object output :"+ set);
    }
}

Output :  TreeMap object output :{Alive is =Awesome, Be in =present, Love=yourself}
                 TreeSet object output :[Alive is Awesome, Be in present, Love yourself]

When to Use TreeMap and TreeSet 

The classic example of using TreeMap and TreeSet is the implementation of dictionary and print it in alphabetical order. You can find the example below :

import java.util.*;

public class Dictionary {
    public static void main(String args[]) {
        TreeMap<String,Set<String>> dictionary = 
                                 new TreeMap<String,Set<String>>();
        Set<String> aWords = new TreeSet<>
                                 (Arrays.asList("Alive","Awesome","All"));
        Set<String> bWords = new TreeSet<>
                                 (Arrays.asList("Be","Banana","Ball"));
        dictionary.put("B", bWords);
        dictionary.put("A", aWords);
        System.out.println(dictionary);
    }
}

Output : {A=[Alive, All, Awesome], B=[Ball, Banana, Be]}

Recap : Difference between TreeMap and TreeSet in Java


TreeMapTreeSet
Interfaceimplements Map interfaceimplements Set interface
DuplicatesAllows duplicate valuesNo
ImplementationRed-black tree based
NavigableMap
implementation
NavigableSet implementation based on a TreeMap.
SortingBased on keysBased on objects

Please mention in the comments in case you have any questions regarding difference between TreeMap and TreeSet in java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry