Java TreeMap get() Method Last Updated : 11 Jul, 2025 Suggest changes Share Like Article Like Report 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() MethodtreeMap.get(Object key)Parameter: key: The method takes one parameter "key" of object type, and refers to the key whose associated value is supposed to be fetched. Returns: The method returns the value associated with the "key" in the parameter, or null if no mapping exists for the key.Examples of Java TreeMap get() MethodExample 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)); } } OutputInitial 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")); } } OutputInitial Mapping: {4=15, Geeks=20, Welcomes=25, You=30} The Value is: 20 The Value is: 30 Important Points:If the key does not exist, get() method returns null.The duplicate keys will be overridden means only the latest value is retained.The treeMap stores keys in natural sorted order i.e. ascending for numbers, alphabetical for strings.The get() method can be used with any object types as long as the keys implement Comparable. C chinmoy lenka Follow Article Tags : Java Java-Collections Java - util package java-TreeMap Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like