TreeMap class

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

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

 

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
  • 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, NULL insertion is not possible
  • TreeMap is non-synchronized
  • Present in java.util package and extends java.util.AbstractMap implements java.util.NavigableSet interface
  • Also, implements java.lang.Cloneable, java.io.Serializable marker interfaces which provides special ability to TreeMap (provided by JVM at run time)

Cloneable: to create a duplicate object or to clone an object

Serializable: to transfer objects across network

34-TreeMap-in-java

Source: Team BenchResources.Net

 

TreeMap constructors:

TreeMap tm = new TreeMap();

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

 

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)

 

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

 

TreeMap tm = new TreeMap(SortedMap m);

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

 

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 but it can be easily converted into synchronized TreeMap using utility method of java.util.Collections class

Map map = Collections.synchronizedMap(tm);

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
https://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-map.html
https://docs.oracle.com/javase/7/docs/api/java/util/NavigableMap.html
https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html
https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html

 

Happy Coding !!
Happy Learning !!

HashMap v/s LinkedHashMap v/s TreeMap
NavigableMap interface