Java TreeMap Class



Introduction

The Java TreeMap class is the Red-Black tree based implementation of the Map interface.Following are the important points about TreeMap −

  • The TreeMap class guarantees that the Map will be in ascending key order.

  • The Map is sorted according to the natural sort method for the key Class, or by the Comparator provided at map creation time, that will depend on which constructor used.

Class declaration

Following is the declaration for java.util.TreeMap class −

public class TreeMap<K,V>
   extends AbstractMap<K,V>
   implements NavigableMap<K,V>, Cloneable, Serializable

Parameters

Following is the parameter for java.util.TreeMap class −

  • K − This is the type of keys maintained by this map.

  • V − This is the type of mapped values.

Class constructors

Sr.No. Constructor & Description
1

TreeMap()

This constructor constructs a new, empty tree map, using the natural ordering of its keys.

2

TreeMap(Comparator<? super K> comparator)

This constructor constructs a new, empty tree map, ordered according to the given comparator.

3

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

This constructor constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys.

4

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

This constructor constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map.

Class methods

Sr.No. Method & Description
1 Map.Entry<K,V> ceilingEntry(K key)

This method returns a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key.

2 K ceilingKey(K key)

This method returns the least key greater than or equal to the given key, or null if there is no such key.

3 void clear()

This method removes all of the mappings from this map.

4 Object clone()

This method returns a shallow copy of this TreeMap instance.

5 boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

6 boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

7 NavigableSet<K> descendingKeySet()

This method returns a reverse order NavigableSet view of the keys contained in this map.

8 NavigableMap<K,V> descendingMap()

This method returns a reverse order view of the mappings contained in this map.

9 Set<Map.Entry<K,V>> entrySet()

This method returns a Set view of the mappings contained in this map.

10 Map.Entry<K,V> firstEntry()

This method returns a key-value mapping associated with the least key in this map, or null if the map is empty.

11 K firstKey()

This method returns the first (lowest) key currently in this map.

12 Map.Entry<K,V> floorEntry(K key)

This method returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

13 K floorKey(K key)

This method returns the greatest key less than or equal to the given key, or null if there is no such key.

14 V get(Object key)

This method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

15 SortedMap<K,V> headMap(K toKey)

This method returns a view of the portion of this map whose keys are strictly less than toKey.

16 Map.Entry<K,V> higherEntry(K key)

This method returns the returns a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key.

17 K higherKey(K key)

This method returns the least key strictly greater than the given key, or null if there is no such key.

18 Set<K> keySet()

This method returns a Set view of the keys contained in this map.

19 Map.Entry<K,V> lastEntry()

This method returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

20 K lastKey()

This method returns the last (highest) key currently in this map.

21 Map.Entry<K,V> lowerEntry(K key)

This method returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.

22 K lowerKey(K key)

This method returns the greatest key strictly less than the given key, or null if there is no such key.

23 NavigableSet<K> navigableKeySet()

This method returns a NavigableSet view of the keys contained in this map.

24 Map.Entry<K,V> pollFirstEntry()

This method removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.

25 Map.Entry<K,V> pollLastEntry()

This method removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.

26 V put(K key, V value)

This method associates the specified value with the specified key in this map.

27 void putAll(Map<? extends K,? extends V> map)

This method copies all of the mappings from the specified map to this map.

28 V remove(Object key)

This method removes the mapping for this key from this TreeMap if present.

29 int size()

This method returns the number of key-value mappings in this map.

30 SortedMap<K,V> subMap(K fromKey, K toKey)

This method returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive

31 SortedMap<K,V> tailMap(K fromKey)

This method returns a view of the portion of this map whose keys are greater than or equal to fromKey.

32 Collection<V> values()

This method returns a Collection view of the values contained in this map.

Methods inherited

This class inherits methods from the following classes −

  • java.util.AbstractMap
  • java.util.Object
  • java.util.Map

Adding and Getting a Value from a TreeMap Example

The following example shows the usage of Java TreeMap get() method to get the value associated with the given key in the map. We've created a TreeMap object of Integer,Integer. Then few entries are added, and using get() we're printing a value for a given key.

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating tree map 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);

      System.out.println("Checking value for key 3");
      System.out.println("Value is: "+ treemap.get(3));
   }    
}

Let us compile and run the above program, this will produce the following result.

Checking value for key 3
Value is: three
Advertisements