Performing Various Operations on TreeMap

After the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the TreeMap. Now, let’s see how to perform a few frequently used operations on the TreeMap.

1. Adding Elements: In order to add an element to the TreeMap, we can use the put() method. However, the insertion order is not retained in the TreeMap. Internally, for every element, the keys are compared and sorted in the ascending order.

// Java program to demonstrate

// the working of TreeMap

import java.util.*;

class GFG {

public static void main(String args[])

{

// Default Initialization of a

// TreeMap

TreeMap tm1 = new TreeMap();

// Initialization of a TreeMap

// using Generics

TreeMap<Integer, String> tm2

= new TreeMap<Integer, String>();

// Inserting the Elements

tm1.put( 3 , "Geeks" );

tm1.put( 2 , "For" );

tm1.put( 1 , "Geeks" );

tm2.put( new Integer( 3 ), "Geeks" );

tm2.put( new Integer( 2 ), "For" );

tm2.put( new Integer( 1 ), "Geeks" );

System.out.println(tm1);

System.out.println(tm2);

}

}

Output:

{1=Geeks, 2=For, 3=Geeks} {1=Geeks, 2=For, 3=Geeks}

2. Changing Elements: After adding the elements if we wish to change the element, it can be done by again adding the element with the put() method. Since the elements in the treemap are indexed using the keys, the value of the key can be changed by simply inserting the updated value for the key for which we wish to change.

// Java program to demonstrate

// the working of TreeMap

import java.util.*;

class GFG {

public static void main(String args[])

{

// Initialization of a TreeMap

// using Generics

TreeMap<Integer, String> tm

= new TreeMap<Integer, String>();

// Inserting the Elements

tm.put( 3 , "Geeks" );

tm.put( 2 , "Geeks" );

tm.put( 1 , "Geeks" );

System.out.println(tm);

tm.put( 2 , "For" );

System.out.println(tm);

}

}

Output:

{1=Geeks, 2=Geeks, 3=Geeks} {1=Geeks, 2=For, 3=Geeks}

3. Removing Element: In order to remove an element from the TreeMap, we can use the remove() method. This method takes the key value and removes the mapping for the key from this treemap if it is present in the map.

// Java program to demonstrate

// the working of TreeMap

import java.util.*;

class GFG {

public static void main(String args[])

{

// Initialization of a TreeMap

// using Generics

TreeMap<Integer, String> tm

= new TreeMap<Integer, String>();

// Inserting the Elements

tm.put( 3 , "Geeks" );

tm.put( 2 , "Geeks" );

tm.put( 1 , "Geeks" );

tm.put( 4 , "For" );

System.out.println(tm);

tm.remove( 4 );

System.out.println(tm);

}

}

Output:

{1=Geeks, 2=Geeks, 3=Geeks, 4=For} {1=Geeks, 2=Geeks, 3=Geeks}

4. Iterating through the TreeMap: There are multiple ways to iterate through the Map. The most famous way is to use a for-each loop and get the keys. The value of the key is found by using the getValue() method.

// Java program to demonstrate

// the working of TreeMap

import java.util.*;

class GFG {

public static void main(String args[])

{

// Initialization of a TreeMap

// using Generics

TreeMap<Integer, String> tm

= new TreeMap<Integer, String>();

// Inserting the Elements

tm.put( 3 , "Geeks" );

tm.put( 2 , "For" );

tm.put( 1 , "Geeks" );

for (Map.Entry mapElement : tm.entrySet()) {

int key

= ( int )mapElement.getKey();

// Finding the value

String value

= (String)mapElement.getValue();

System.out.println(key + " : "

+ value);

}

}

}

Output:

1 : Geeks 2 : For 3 : Geeks

Features of a TreeMap:

Some important features of the treemap are:

  1. This class is a member of [ava Collections Framework.
  2. The class implements Map interfaces including NavigableMap, SortedMap and extends AbstractMap class.
  3. TreeMap in Java does not allow null keys (like Map) and thus a NullPointerException is thrown. However, multiple null values can be associated with different keys.
  4. Entry pairs returned by the methods in this class and its views represent snapshots of mappings at the time they were produced. They do not support the Entry.setValue method.