This example shows how to get a value for a key from HashMap in Java using the get method. The example also shows how to get a default value if the key does not exist using the getOrDefault method of the HashMap class.
How to get a value for the given key from HashMap using the get method?
The get
method of the HashMap class returns the value mapped to a given key if the key exists in the HashMap object.
1 |
public V get(Object key) |
If the key does not exist in the map, 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 24 25 26 27 28 |
import java.util.HashMap; public class HashMapGetExample { public static void main(String[] args) { //create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, "Two"); hMapNumbers.put(3, "Three"); /* * This will return "Two" as the key 2 is * mapped to value "Two" in the HashMap */ System.out.println( hMapNumbers.get(2) ); /* * This will return null because the key 4 * does not exist in the HashMap object */ System.out.println( hMapNumbers.get(4) ); } } |
Output
1 2 |
Two null |
However, do not rely on the null value to determine whether the key exists in the map object. Since the HashMap class allows null values, the get
method can return null if the key is mapped to a null value in the map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, null); hMapNumbers.put(3, "Three"); /* * This will return null because the key 2 * is mapped to a null value in the HashMap object */ System.out.println( hMapNumbers.get(2) ); /* * This will also return null because the key 4 * does not exist in the HashMap object */ System.out.println( hMapNumbers.get(4) ); |
Output
1 2 |
null null |
If you want to check if the key exists in the HashMap, you should use the containsKey
method instead of the get
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, null); hMapNumbers.put(3, "Three"); /* * This will return true as the key 2 * exists in the map */ System.out.println( hMapNumbers.containsKey(2) ); /* * This will return false as the key 4 * does not exist in the map */ System.out.println( hMapNumbers.containsKey(4) ); |
Output
1 2 |
true false |
How to get a default value if the key does not exist in the HashMap using the getOrDefault method?
As we have seen in the above example, the get
method returns a null value if the key does not exist in the map. If you want to get a provided default value if the key does not exist in the map, you can use the getOrDefault
method.
1 |
public V getOrDefault(Object key, V defaultValue) |
The getOrDefault
method returns a value mapped to a given key if the key exists in the map. If the key does not exist in the map, the getOrDefault
method returns the specified default value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
//create new HashMap HashMap<Integer, String> hMapNumbers = new HashMap<Integer, String>(); //add key-value pairs hMapNumbers.put(1, "One"); hMapNumbers.put(2, null); hMapNumbers.put(3, "Three"); /* * This will return "One" as the key 1 * is mapped to value "One" */ System.out.println( hMapNumbers.getOrDefault(1, "Not Found") ); /* * This will return null as the key 2 * is mapped to value null */ System.out.println( hMapNumbers.getOrDefault(2, "Not Found") ); /* * This will return "Not Found" as the key 4 * is not mapped to any value in the map */ System.out.println( hMapNumbers.getOrDefault(4, "Not Found") ); |
Output
1 2 3 |
One null Not Found |
This example is a part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.