TreeMap Interface:

  • A TreeMap contains values based on the key.
  • It implements the NavigableMap interface.
  • It contains only unique elements.
  • It cannot have null key but can have multiple null values.
  • Insertion order is preserved.

Let's see a program:

TreeMapDemo.java:

import java.util.*;
classTreeMapDemo
{
public static void main(String args[])
{
TreeMaphm=new TreeMap();
hm.put(100,"Tapuuu");
hm.put(102,"Silan");
hm.put(101,"Sushh");
hm.put(103,"Manu");
for(Map.Entry m:hm.entrySet())
{
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

Output

100 Tapuuu
101 Sushh
102 Silan
103 Manu

HashMap vs. TreeMap:

 

HashMap is can contain one null key. TreeMap can not contain any null key.
HashMap maintains no order. TreeMap maintains ascending order.
Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment