Difference Between TreeSet and TreeMap

An array is used to store a set of data elements of the same type. Most programming languages support Arrays. Even though an array can store multiple values; there is a major disadvantage. Once the array is created, it is not possible to change it. If the programmer declared an array of 10 elements, then he cannot store 15 elements. When the programmer declares an array of 10 elements and stores only 5 elements, the rest of the allocated memory is a waste. Programming languages such as Java has Collections to store data elements dynamically. There is a number of collections. Collections help to perform adding, removing elements and other operations. The base interface is known as Collection. Set, List and Queue are some interfaces that extend the Collection interface. Map is an interface of collection hierarchy, but it does not extend the Collection interface.  TreeSet is a class that implements the Set interface and store the elements in ascending order.  TreeMap is a class that implements Map interface and stores key, value pairs in ascending order. That is the key difference. This article discusses the difference between TreeSet and TreeMap.

CONTENTS

1. Overview and Key Difference
2. What is TreeSet
3. What is TreeMap
4. Similarities Between TreeSet and TreeMap
5. Side by Side Comparison – TreeSet vs TreeMap in Tabular Form
6. Summary

What is TreeSet?

The TreeSet is a class that implements Set interface. The TreeSet maintains unique elements. TreeSet implements NavigableSet interface. The Navigable interface extends SortedSet, Set, Collection and Iterable interfaces in hierarchical order. TreeSet stores the elements in ascending order. If the order of insertion is A,C,B, the TreeSet will store them as A,B,C. There are methods of TreeSet. The add method is used to add an element to the Set. The remove method is used to remove a specified element. The clear method is used to remove all elements. The contains method returns a true if the specified element is present in the Set. They are some methods provided by TreeSet. Refer the below program.

Figure 01: Program using TreeSet

According to the above program, Treeset is an object of type TreeSet. It can store Strings. The elements are added using add method. The insertion order is A, C,D and B. Using the iterator, the stored values are printed to the screen. The elements are stored in the order A,B,C,D. Therefore, the TreeSet maintains an ascending order of the elements of the Set. If there is another element as “D” it will not print because the element D already exists in the Set. It always stores unique elements.

What is TreeMap?

TreeMap is a class that implements Map interface. Map supports key-value pairs. Each key, value pair is an entry. Every key is unique and has a corresponding value. The containsKey method is used to find a specific key while containsValue method is used to find a specific value. The get method is used to find the value corresponding to the given key. The put method is used to store a value with the given key.  It is also possible to remove an element at a specific key using the remove method.  Those are some common methods of Map interface. It helps to search, insert and delete elements based on the key. TreeMap class implements NavigableMap. NavigableMap extends SortedMap. SortedMap extends Map.  Therefore, the methods of Map can be used with TreeMap. Refer the below program.

Figure 02: Program using TreeMap

According to the above program, an object of TreeMap is created. The programmer can add elements using the object.  The put method is used to insert key, value pairs. The get method is used with the specific key to fetch the elements. The programmer can use Map.Entry to print all keys and values. When observing the output, it does not maintain the inserted order. It stores the elements in ascending order.

What are the Similarities Between TreeSet and TreeMap?

  • Both TreeSet and TreeMap are in the collection hierarchy.
  • Both TreeSet and TreeMap maintain the ascending order.
  • Both TreeSet and TreeMap can store and manipulate many elements.

What is the Difference Between TreeSet and TreeMap?

TreeSet vs TreeMap

TreeSet is a class that implements the Set interface and store the elements in ascending order. TreeMap is a class that implements Map interface and stores key, value pairs in ascending order.
 Implemented Interface
TreeSet implements Set interface. TreeMap implements Map interface.

Summary – TreeSet vs TreeMap

An array is used to store a set of elements, but it does not help to store elements dynamically. Programming languages such as Java contains Collections to store data elements dynamically. Collection is the base class in collection hierarchy. It consists of classes and interfaces to perform operations such as adding, deleting elements. Set and Map are two interfaces of Collection hierarchy. TreeSet is a class that implements the Set interface and store the elements in ascending order.  TreeMap is a class that implements Map interface and stores key, value pairs in ascending order. That is the difference between TreeSet and TreeMap.

Reference:

1.“TreeSet in Java – javatpoint.” , Java Point. Available here
2.“TreeMap in Java – javatpoint.”, Java Point. Available here