A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-treemap-get-method/ below:

Java TreeMap get() Method - GeeksforGeeks

Java TreeMap get() Method

Last Updated : 11 Jul, 2025

The get() method of the TreeMap class in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. If the key does not exist in the map, the method returns null.

Syntax of TreeMap get() Method

treeMap.get(Object key)

Examples of Java TreeMap get() Method

Example 1: In this example, we are going to create a TreeMap with integer keys and string values, and then we will fetch the values for specific keys using the get() method.

Java
// Java program to demonstrate 
// the get() method of TreeMap
import java.util.*;

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

        // Create an empty TreeMap
        TreeMap<Integer, String> tm = new TreeMap<Integer, String>();

        // Map string values to int keys
        tm.put(10, "Geeks");
        tm.put(15, "4");
        tm.put(20, "Geeks");
        tm.put(25, "Welcomes");
        tm.put(30, "You");

        System.out.println("Initial mapping: " + tm);

        // Get the value of key 25
        System.out.println("The Value is: " + tm.get(25));

        // Get the value of key 10
        System.out.println("The Value is: " + tm.get(10));
    }
}

Output
Initial mapping: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}
The Value is: Welcomes
The Value is: Geeks

Example 2: In this example, we are going to create a TreeMap with string keys and integer values.

Java
// Java program to demonstrate 
// the get() method of TreeMap
import java.util.*;

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

        // Create an empty TreeMap
        TreeMap<String, Integer> tm = new TreeMap<String, Integer>();

        // Map int values to string keys
        tm.put("Geeks", 10);
        tm.put("4", 15);
        tm.put("Geeks", 20); 
        tm.put("Welcomes", 25);
        tm.put("You", 30);

        System.out.println("Initial Mapping: " + tm);

        System.out.println("The Value is: " + tm.get("Geeks"));

        System.out.println("The Value is: " + tm.get("You"));
    }
}

Output
Initial Mapping: {4=15, Geeks=20, Welcomes=25, You=30}
The Value is: 20
The Value is: 30

Important Points:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4