Java – TreeMap class with example

In this article, we will discuss TreeMap class – the implementation class for NavigableMap interface in detail

1. Key points about TreeMap:

  • TreeMap doesn’t allow duplicate keys
  • Maintains ascending sorting-order, by-default
  • For pre-defined classes like String and wrapper classes, etc.

2. TreeMap:

  • TreeMap is the implementation class of NavigableMap interface (i.e.; TreeMap implements NavigableMap)
  • Uses red-black-tree to store key-value pairs (i.e.; map entries)
  • Insertion-order is NOT maintained
  • It allows only unique keys to be inserted
  • Stores key-value pairs in sorting order on the basis of keys only, not values
  • TreeMap stores key-value pairs in sorting-order; it could be either default natural sorting-order or programmer defined customized sorting-order
  • Keys inserted should be of comparable type, otherwise ClassCastException will be thrown
  • At any time, TreeMap contains only unique keys and there is no restriction on corresponding values
  • Allows NULL insertion but maximum of only one NULL key up-to Java 1.6 version
  • From Java 1.7 version, even one NULL insertion is not possible
  • TreeMap is non-synchronized
  • Present in java.util package and extends java.util.AbstractMap implements java.util.NavigableMap interface
  • Also, implements java.lang.Cloneable, java.io.Serializable marker interfaces which provides special ability to TreeMap (provided by JVM at run time) like,
  • java.lang.Cloneable: to create a duplicate object or to clone an object
  • java.io.Serializable: to transfer objects across network
34-TreeMap-in-java

Source: Team BenchResources.Net

3. TreeMap constructors:

3.1 TreeMap tm = new TreeMap();

  • creates an empty TreeMap object
  • where key-value pairs will be inserted according to default natural sorting-order of keys

3.2 TreeMap tm = new TreeMap(Comparator c);

  • creates an empty TreeMap object
  • where key-value pairs will be inserted according to specified comparator object (i.e.; customized sorting-order)

3.3 TreeMap tm = new TreeMap(Map m);

  • creates an equivalent TreeMap object for the specified map
  • it is basically used for inter-conversion between map objects

3.4 TreeMap tm = new TreeMap(SortedMap m);

  • create an equivalent TreeMap object for the specified SortedMap
  • follows same ordering as per specified SortedMap

4. TreeMap examples:

TreeMapStringExample.java

package in.bench.resources.java.collection;

import java.util.TreeMap;

public class TreeMapIntegerStringExample {

	public static void main(String[] args) {

		// creating NavigableMap reference and TreeMap object
		TreeMap<Integer, String> tm = new TreeMap<Integer, String>();

		// adding key-value pairs to SortedMap/TreeMap object
		tm.put(3, "Yahoo");
		tm.put(4, "Amazon");
		tm.put(7, "Twiter");
		tm.put(1, "Google");
		tm.put(5, "Reddit");
		tm.put(6, "LinkedIn");
		tm.put(2, "Facebook");

		// printing according to natural ordering
		System.out.println("Elements sorted "
				+ "according to Natural ordering:\n");
		System.out.println(tm);
}
}

Output:

Elements sorted according to Natural ordering:

{1=Google, 2=Facebook, 3=Yahoo, 4=Amazon, 5=Reddit, 6=LinkedIn, 7=Twiter}

Note: All methods of TreeMap is non-synchronized

Q) How to make TreeMap synchronized ?

  • but it can be easily converted into synchronized TreeMap
  • Using utility method synchronizedMap(tm); of java.util.Collections class
Map map = Collections.synchronizedMap(tm);

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - HashMap v/s LinkedHashMap v/s TreeMap
Java 6 - NavigableMap interface