Java treemap

Hey guys!! Welcome to flower brackets blog. Today we are going to learn java treemap.

  • TreeMap class is member of java collection framework.
  • TreeMap retain ascending order.
  • TreeMap is red black tree based implementation.
  • TreeMap do not have “null” keys. Meanwhile different keys are related to various null values.
  • TreeMap is sorted according to the natural ordering of its keys.
  • TreeMap contain only unique elements.
  • TreeMap implementation is not synchronized. Because if map access multiple threads concurrently then one of the threads should be accessed externally using SortedMap sm = Collections.synchronizedSortedMap(new TreeMap).

Hierarchy of TreeMap Class

java treemap

Treemap class belongs to java.util package.

public class TreeMap<K,V>

  • K is type of keys to maintain treemap.
  • V is a type of values maintain the treemap.
java.lang.Object
     java.util.AbstractMap<K,V>
          java.util.TreeMap<K,V>

Treemap class extends abstractmap class, abstractmap class extends object class.

Serializable, Cloneable, Map<K,V>, NavigableMap<K,V>, SortedMap<K,V>


Let’s see a java program,

import java.util.Map;
import java.util.TreeMap;

public class TreemapExample 
{ 
   public static void main(String[] args) 
   {
      TreeMap<Integer,String> tm = new TreeMap<Integer,String>(); 
      tm.put(3135,"Ajinkya"); 
      tm.put(3133,"Rohit"); 
      tm.put(3132,"Vijay"); 
      tm.put(3134,"Rahul"); 

      for(Map.Entry me : tm.entrySet())
      { 
         System.out.println(me.getKey() + " " + me.getValue()); 
      }
   }
}

Output :

3132 Vijay
3133 Rohit
3134 Rahul
3135 Ajinkya


Methods of treemap

Methods - modifierDescription
Object firstKey()returns the first (lowest) key currently in this sorted map
Object get(Object key)returns the value to which this map maps the specified key
Object clone()This method returns a shallow copy of this TreeMap instance
void clear()This method removes all mappings from this TreeMap and clears the map.
boolean containsKey(Object key)Returns true if this map contains a mapping for the specified key.
boolean containsValue(Object value)Returns true if this map contains a mapping for the specified value.
NavigableSet descendingKeySet()Returns a reverse order NavigableSet view of the keys contained in this map.
NavigableMap descendingMap()Returns a reverse order view of the mappings contained in this map.
Set entrySet()returns a Set() view of the mappings contained in this map.
Map.Entry firstEntry()Returns a key value mapping associated with the least key in this map or null if the map is empty.
Map.Entry floorEntry(K key)Returns a key value mapping associated with the greatest key <= the given key or null if there is no such key.
K floorKey(K key)Returns the greatest key <= the given key or null if there is no such key.
V get(Object key)Returns the value to which the specified key is mapped or null if this map contains no mapping for the key.
SortedMap headMap(K toKey)Returns a view of the portion of this map whose keys are strictly less than toKey.
NavigableMap headMap(K toKey, boolean inclusive)Returns a view of the portion of this map whose keys are less than ( or equal to, if inclusive is true ) toKey.
Map.Entry higherKey(K key)Returns a key-value mapping associated with the least key strictly greater than the given key or null if there is no such key.
K higherKey(K key)Returns the least key strictly greater than the given key or null if there is no such key.
Set keySet()Returns a Set view of the keys contained in this map.
Map.Entry lastEntry()Returns a key-value mapping associated with the greatest key in this map or null if the map is empty.
Object lastKey()Returns the last(highest) key currently in this map.
Map.Entry lowerEntry(K key)Returns a key value mapping associated with the greatest key strictly less than the given key or null if there is no such key.
K lowerKey(K key)Returns the greatest key strictly less than the given key or null if there is no such key.
NavigableSet navigableKeySet()Returns a NavigableSet view of the keys contained in this map.
Map.Entry pollFirstEntry()Removes and returns a key value mapping associated with the least key in this map or null if the map is empty.
Map.Entry pollLastEntry()Removes and returns a key value mapping associated with the greatest key in this map or null if the map is empty.
Object put(Object key, Object value)This method is used to insert a mapping into a map.
void putAll(Map map)Copies all of the mappings from the specified map to this map.
Object remove(Object key)removes the mapping for this key from this TreeMap if present.
int size()returns the number of key value mappings in this map.
NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)Returns a view of the portion of this map whose keys range fromKey to toKey.
SortedMap subMap(K fromKey, K toKey)Returns a view of the portion of this map whose keys range from fromKey, inclusive to toKey, exclusive.
SortedMap tailMap(K fromKey)Returns a view of the portion of this map whose keys are greater than or equal to fromKey.
NavigableMap tailMap(K fromKey, boolean inclusive)Returns a view of the portion of this map whose keys are greater than ( or equal to, if inclusive is true ) fromKey.
Collection values()Returns a Collection view of the values contained in this map.

Java treemap constructors

  • public TreeMap()

Using this constructor empty treemap is constructed. It will be sorted using natural order of its keys.


  • public TreeMap(Map<? extends K, ? extends V> m)

this constructor accepts map and sorted using natural order of keys.


  • public TreeMap(SortedMap<K, ? extends V> m)

this constructor accepts treemap and initializes treemap with entries from sortedmap. It will be sorted in the same order as sortedmap.


  • public TreeMap(Comparator<? super K> comparator)

this constructor accepts Comparator object and constructs an empty treemap which sorts using Comparator.


Reference – Oracle Docs

Related Posts