TreeSet Vs TreeMap in Java

TreeSet is an implementation class of NavigableSet. In a TreeSet, duplicate as well as heterogenous (values of different data types) values are not allowed. If you’re trying to insert heterogeneous objects then it displays a ClassCastException. If the tree set exists, then we can insert a null value, and if the treeset doesn’t exist then we cannot insert a null value in it. All versions of Java below 6.0 allows insertion of null value, but all other versions show Null Pointer Exception.

Here, we will take a look at the collection framework of TreeSet.

Hierarchy of TreeSet Class

                                                          TreeSet

From the above diagram, we infer that the treeset inherits all the properties of all sets present above it as well as the collection framework. The Navigable Set inherits all the properties of all sets present above as well as of the collection framework. This chain of inheritance goes up to the collection framework.

There are Four Styles of TreeSet Constructor

  1. TreeSet(): Constructs a new, empty tree set, sorted in line with the natural ordering of its elements.
  2. TreeSet(Comparator c): Constructs a new, empty tree set, sorted as per the required comparator.
  3. TreeSet(Collection c): Create a new tree set that stores the elements in the specified collection, sorted according to the natural ordering of its elements.
  4. TreeSet (SortedSet): Create a new tree set containing the identical elements and using the identical ordering because of the specified sorted set.

Program for TreeSet

import java.util.*;

class Example{

 public static void main(String[] args)
 {

     TreeSet<Integer> t = new TreeSet<Integer>();
     t.add(10);
     t.add(20);
     t.add(30);
     System.out.println(t);

 }

}

Output:

[10, 20, 30]

Now, we will discuss TreeMap.

TreeMap stores data in key and value pair format. You can find any value using its key. All keys should have unique values while the values it contains may or may not be unique. In a treemap, the key cannot be null but it can have multiple null values. It is a sorted map that maintains the sorting order by using its key. It does not follow the hashing concept. TreeMap is a red-black tree-based navigable map.

Hierarchy of TreeMap Class                                                      TreeMap

In the above diagram, the sorted map inherits all the properties of the map. A navigable map inherits all the properties of a sorted map, which means the data should be in logical order. And, TreeMap inherits all the properties of a navigable map, which implies heterogeneous elements are not allowed. So basically, the treemap inherits all the properties of the map, sorted map, and navigable map.

java.lang.Object

      java.util.AbstractMap<K, V>

             java.util.TreeMap<K, V>

Java treemap class extends the abstract class and abstract class extends object class.

Types of Parameters:

K – key introduce by treemap

V – mapped values

All implemented interfaces:

Serializable, Cloneable, Map<K, V>, NavigableMap<K, V>, SortedMap<K, V>.

There are Four Styles of TreeMap Constructor

  1. TreeMap(): Constructs a new, empty treemap, sorted in line with the natural ordering of its keys.
  2. TreeMap(Comparator<?super K> comparator): Constructs a new, empty treemap, sorted per the required comparator.
  3. TreeMap(Map<?extends K,?extends V>m): Constructs a new tree map with the entries from m, sorted according to the natural ordering of its elements.
  4. TreeMap(SortedMap<K,? extends V> m): Constructs a new tree map with the entries from the SortedMap, which will be stored data in the same order as SortedMap.

Program for TreeMap

import java.util.*;

class Example {
    public static void main(String[] args) {
        TreeMap<Integer,Integer> t = new TreeMap<Integer, Integer>();
        t.put(1,4);
        t.put(2,9);
        t.put(3,11);
        System.out.println(t);
    }
}

Output:

{1 = 4, 2 = 9, 3 = 11}

TreeSet vs TreeMap

TreeSet TreeMap
It implements a set interface. It implements a map interface.
It contains only one object. It stores two objects called key and value.
Duplicate values are not allowed in it. Key should not be duplicate while value can be duplicated.
It implements Navigable Set. It implements Navigable Map.
It is sorted based on objects. It is sorted based on keys.
It is implemented using a TreeMap. It is itself implemented using a red-black tree because red-black is a self-balancing binary tree.
If a tree set exists then we can insert a null value, instead, if the treeset doesn’t exist then we cannot insert a null value in it. Although it is allowed till Java verison 6.0. The key cannot be null but it can have multiple null values.

So, that’s all about TreeSet and TreeMap in Java. Comment down below if you have any queries.

Leave a Comment

Your email address will not be published. Required fields are marked *