«Back to Home

Core Java

Topics

TreeMap Class In Java

TreeMap Class
 
In Java, TreeMap class holds the values, based on the key. It implements the NavigableMap interface and extends AbstractMap class. It contains the unique elements only and cannot have null key but can have multiple null values. It maintains an ascending order.
 
Constructors of TreeMap Class

TreeMap( )

This constructor is used to construct an empty tree map, which will be sorted by using the natural order of its keys.
 
TreeMap(Comparator comp)

This constructor is used to construct an empty tree-based map, which will be sorted by using the Comparator comp.
 
TreeMap(Map m)

This constructor is used to initialize a tree map with the entries from m, which will be sorted by using the natural order of the keys.
 
TreeMap(SortedMap sm)

This constructor is used to initialize a tree map with the entries from the SortedMap sm, which will be sorted in the same order as sm.
 
Methods of TreeMap Class

void clear()

This method is used to remove all the mappings from the TreeMap.
 
Object clone()

This method is used to return a shallow copy of the TreeMap instance.
 
Comparator comparator()

This method is used to return the comparator, used to order this map or null, if the map uses its keys' natural order.
 
boolean containsKey(Object key)

This method is used to return true, if the map contains a mapping for the specified key.
 
boolean containsValue(Object value)

This method is used to return true, if the map maps one or more keys to the specified value.
 
Set entrySet()

This method is used to return a set view of the mappings contained in the map.
 
Object firstKey()

This method is used to return the first (lowest) key, currently in the sorted map.
 
Object get(Object key)

This method is used to return the value to which the map maps the specified key.
 
SortedMap headMap(Object toKey)

This method is used to return a view of the portion of the map whose keys are strictly less than toKey.
 
Set keySet()

This method is used to return a Set view of the keys, contained in the map.
 
Object lastKey()

This method is used to return the last (highest) key, currently in the sorted map.
 
Object put(Object key, Object value)

This method is used to associate the particular value with the particular key in the map.
 
void putAll(Map map)

This method is used to copy all the mappings from the specified map to the map.
 
Object remove(Object key)

This method is used to remove the mapping for the key from the TreeMap, if present.
 
int size()

This method is used to return the number of key-value mappings in the map.
 
SortedMap subMap(Object fromKey, Object toKey)

This method is used to return a view of the portion of the map whose keys range from fromKey, inclusive, to toKey, exclusive.
 
SortedMap tailMap(Object fromKey)

This method is used to return a view of the part of the map whose keys are greater than or equal to fromKey.
 
Collection values()

This method is used to return a collection view of the values contained in the map.
 
Let’s see an example, given below.
 
Code
  1. import java.util.*;  
  2. public class CollectionsExample {  
  3.     public static void main(String args[]) {  
  4.         TreeMap < Integer, String > t = new TreeMap < Integer, String > ();  
  5.         t.put(160"Harry");  
  6.         t.put(182"Mia");  
  7.         t.put(141"Sophia");  
  8.         t.put(163"David");  
  9.         for (Map.Entry m: t.entrySet()) {  
  10.             System.out.println(m.getKey() + " " + m.getValue());  
  11.         }  
  12.     }  
  13. }  
48

Output

49

Difference between HashMap and TreeMap
 
HashMap

HashMap can hold one null key. It maintains no order.
 
TreeMap

TreeMap cannot hold any null key. It maintains an ascending order.
 
Summary

Thus, we learnt that Java TreeMap class holds the values, based on the key. It implements the NavigableMap interface and extends AbstractMap class. It holds the unique elements only and also learnt how to create it in Java.