Difference between TreeSet and TreeMap in Java?

TreeSet and TreeMap are both classes in Java that implement the Set and Map interfaces, respectively. They are both sorted collections, which means the elements in the collections are stored in a sorted order.

The main difference between the two is that a TreeSet is a set of unique elements, while a TreeMap is a map that associates a key with a value.

TreeSet:

  • Stores unique elements
  • Elements are stored in a sorted order based on their natural ordering or based on a custom Comparator provided at the time of creation
  • Does not allow null elements
  • Implements the Set interface

TreeMap:

  • Stores key-value pairs, where each key is unique
  • Elements are stored in a sorted order based on the keys’ natural ordering or based on a custom Comparator provided at the time of creation
  • Allows null values, but not null keys
  • Implements the Map interface

In summary, if you want to store a set of unique elements in a sorted order, use a TreeSet. If you want to store key-value pairs in a sorted order, use a TreeMap.

TreeSet Example

import java.util.TreeSet;

public class TreeSetExample {
  public static void main(String[] args) {
    TreeSet<String> treeSet = new TreeSet<>();

    // Add elements to the TreeSet
    treeSet.add("apple");
    treeSet.add("banana");
    treeSet.add("orange");

    // Print the TreeSet
    System.out.println(treeSet);

    // Remove an element from the TreeSet
    treeSet.remove("banana");

    // Print the TreeSet again
    System.out.println(treeSet);
  }
}

Output below, TreeSet automatically sorts its elements in natural order, so the output may vary depending on the order in which the elements are added.

[apple, banana, orange] [apple, orange]

TreeMap Example

import java.util.TreeMap;

public class TreeMapExample {
    public static void main(String[] args) {
        // create a new TreeMap to store integers as keys and strings as values
        TreeMap<Integer, String> treeMap = new TreeMap<>();

        // add some key-value pairs to the TreeMap
        treeMap.put(3, "three");
        treeMap.put(1, "one");
        treeMap.put(2, "two");

        // print out the TreeMap
        System.out.println(treeMap);

        // get the value associated with a key
        String value = treeMap.get(1);
        System.out.println("The value associated with key 1 is " + value);

        // remove a key-value pair
        treeMap.remove(2);

        // print out the TreeMap again
        System.out.println(treeMap);
    }
}

Output below, TreeMap automatically sorts its keys in natural order, so the output may vary depending on the order in which the key-value pairs are added.

Okay which one to use, TreeSet or TreeMap?

TreeMap and TreeSet are both implementations of the SortedMap and SortedSet interfaces, respectively, in Java. They both use a self-balancing binary search tree (specifically, a red-black tree) to store their elements in sorted order.

Here are some guidelines for when to use TreeMap and TreeSet:

Use TreeMap when:

  • You need to store key-value pairs in sorted order by key
  • You need to look up values by key
  • You need to iterate over the keys or key-value pairs in sorted order

Use TreeSet when:

  • You need to store elements in sorted order
  • You need to look up elements by value
  • You need to iterate over the elements in sorted order

Leave a comment