wellnwill Marathahalli Bangalore

TreeMap

The basic data structure is tree and based on tree data structure, it will be decided that where to store or search for keys.

  • Keys will be stored based on some sorting order (either default sorting order or user defined sorting order).
  • Internally compareTo() method is used to check object uniqueness i.e comparision of objects for sorting purpose.

The object which will be added in TreeMap as key must be Comparable type and homogeneous.

If key object is non-homogeneous, we will get Runtime Exception i.e. ClasscastException.

We can insert duplicate values including null but key must be unique and not null.

It stores the data into sorted order of keys.

An object of this class can be used to manage the entries in ascending order of keys with value.

We cannot add null as key.

import java.util.*;
class Test280
{
 public static void main(String args[])
 {
  TreeMap tm=new TreeMap();
  tm.put("003","John");
  tm.put("001","Robert");
  tm.put("002","Mac");
  tm.put("004","David");
  System.out.println(tm);
}
}

Footer with Map

Top