TreeMap Class in Java Tutorial with Example

TreeMap class implements the Map interface.It extends AbstractMap class and implements NavigableMap interface. It has the following properties:

1. TreeMap sorts the keys according to the natural ordering of its keys, or by Comparator provided at  map creation time.
2. TreeMap is a red black tree based NavigableMap implementation.
3. TreeMap implementation provides guaranteed log(n) time cost for the containsKey,get,put and remove operations.
4. TreeMap does not allow null keys and null values.
5. TreeMap is not synchronized.It must be synchronized externally
    SortedMap  m = Collections.synchronizedSortedMap(new TreeMap(...));

TreeMap Example


TreeMap guarantees that its elements will be sorted in an ascending key order.

import java.util.*;

public class TreeMapExample {

   public static void main(String args[]) {

      // Declare TreeMap 
      TreeMap<Integer, String> treemap = 
             new TreeMap<Integer, String>();

      //Adding elements to TreeMap
      treemap.put(1, "Value1");
      treemap.put(44,"Value2");
      treemap.put(22,"Value3");
      treemap.put(5, "Value4");
      treemap.put(2, "Value5");

      // Display elements using Iterator
      Set set = treemap.entrySet();
      Iterator iterator = set.iterator();
      while(iterator.hasNext()) {
         Map.Entry pair = (Map.Entry)iterator.next();
         System.out.print("key is: "+ pair.getKey() + " and value is: ");
         System.out.println(pair.getValue());
      }
   }
}

Output :
key is: 1 and value is: Value1
key is: 2 and value is: Value5
key is: 5 and value is: Value4
key is: 22 and value is: Value3
key is: 44 and value is: Value2

TreeMap Methods

1. public void clear() : it removes all of the mappings from the map.
2. public Object clone() : This method returns the shallow copy of TreeMap instance.
3. public boolean containsKey(Object key) : it returns true if the map contains the mapping for the specified key.
4. public boolean containsValue(Object value) : it returns true if the map maps one or more keys to the specified value.
5. public V get(Object key) : it returns the value to which the specified key is mapped,or null if the map contains no mapping for the key.
6. public Set keySet() : it returns a Set view of the keys contained in the map.
7. public V put(K  key,V   value) : it associates the specified value with the specified key in the map.
8. public V remove(Object key) : it removes the mapping for the key from the TreeMap if present.
9. public int size() : it returns the number of key-value mappings in the map.

TreeMap Tutorials



  • TreeMap Iterator Example
  • Sort TreeMap by Value
  • Iterate TreeMap Keys in Reverse Order
  • TreeMap vs HashMap
  • How TreeMap works in Java : TreeMap Interview Questions


  • References : TreeMap Oracle Docs

    About The Author

    Subham Mittal has worked in Oracle for 3 years.
    Enjoyed this post? Never miss out on future posts by subscribing JavaHungry