This example shows how to get a value for a key from TreeMap using the get method in Java. This example also shows how to get the default value for a key if the key does not exist using the getOrDefault method.
How to get a value for a key from TreeMap using the get method in Java?
The get
method of the TreeMap class returns a value mapped to a given key in the TreeMap object.
1 |
public V get(Object key) |
The get
method returns the value associated with the key if the key exists in the Treemap. If the key does not exist, the get
method returns null.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.TreeMap; public class TreeMapGetExample { public static void main(String[] args) { TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * To get a value mapped to a key, use the * get method. */ //this will return "Green", the value mapped to key 2 System.out.println( tmapColors.get(2) ); //this will return null as key 4 does not exist System.out.println( tmapColors.get(4) ); } } |
Output
1 2 |
Green null |
As you can see from the output, the get
method returns null if the specified key does not exist in the map object. However, it may also return null if the specified key is mapped to a null value.
1 2 3 4 5 6 7 8 9 10 11 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, null); //this will return null as the key 3 is mapped to null System.out.println( tmapColors.get(3) ); //this will also return null as the key 4 does not exist System.out.println( tmapColors.get(4) ); |
Output
1 2 |
null null |
For this reason, do not use the get
method to check if the key exists in the TreeMap as it produces unreliable results in case there are null values in the map. Instead, always use the containsKey
method for checking the existence of the key.
How to get a default value if the key does not exist using the getOrDefault method?
The getOrDefault
method returns the specified value if the given key does not exist in the TreeMap object.
1 |
public V getOrDefault(Object key, V defaultValue) |
The getOrDefault
method returns a value mapped to a key if the key exists in the map. If it does not exist, the getOrDefault
method returns the specified default value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, null); //this will return "Red" as the key 1 is mapped to "Red" System.out.println( tmapColors.getOrDefault(1, "Default") ); //this will return null as the key 3 is mapped to null System.out.println( tmapColors.getOrDefault(3, "Default") ); //this will return "Default" as the key 4 does not exist System.out.println( tmapColors.getOrDefault(4, "Default") ); |
Output
1 2 3 |
Red null Default |
As you can see from the example, the getOrDefault
method returned the “Default” string for the key 4 which does not exist in the TreeMap object.
Please also visit how to add new key-value mappings to the TreeMap object example to know more.
This example is a part of the Java TreeMap Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap