This example shows how to check if key exists in HashMap in Java using the get and containsKey methods of the HashMap class. The example also shows how to check if HashMap has the key using the best approach.
How to check if the key exists in HashMap in Java?
There are a couple of ways using which you can check if the HashMap contains a key.
1. How to check if HashMap contains the key using the get method?
The get
method of the HashMap class returns the value mapped to a given key in the map. If the key is not mapped to any value in the map, it returns null.
1 |
public V get(Object key) |
You can compare the return value with null to check if the HashMap has the key.
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 29 30 31 |
import java.util.HashMap; public class HashMapContainsKeyExample { public static void main(String[] args) { 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" String value = hMapNumbers.get( 2 ); if( value == null ) System.out.println("Key does not exist"); else System.out.println("Key exists"); /* * This will return null, as the HashMap does not have * the mapping for key 4 */ value = hMapNumbers.get( 4 ); if( value == null ) System.out.println("Key does not exist"); else System.out.println("Key exists"); } } |
Output
1 2 |
Key exists Key does not exist |
Important Note:
This approach works only if the HashMap does not contain null values mapped to the keys. If the HashMap contains null values, the get
method returns null for those keys as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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 "Two" String value = hMapNumbers.get( 2 ); if( value == null ) System.out.println("Key does not exist"); else System.out.println("Key exists"); |
Output
1 |
Key does not exist |
As you can see from the output, even if the HashMap has the key 2, our program printed “Key does not exist” because it was mapped to a null value.
2. How to check If HashMap has the key using the containsKey method?
The containsKey
method of the HashMap class returns true if the HashMap contains the specified key, false otherwise.
1 |
public boolean containsKey(Object value) |
The containsKey
method returns true even if the key is mapped to a null value as given below.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.HashMap; public class HashMapContainsKeyExample { public static void main(String[] args) { 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 * HashMap contains key 2 */ boolean keyExists = hMapNumbers.containsKey( 2 ); if( keyExists ) System.out.println("Key exists"); else System.out.println("Key does not exist"); /* * this will return true, as the * HashMap has key 3 */ keyExists = hMapNumbers.containsKey( 3 ); if( keyExists ) System.out.println("Key exists"); else System.out.println("Key does not exist"); /* * This will return false, as the * HashMap does not contain key 4 */ keyExists = hMapNumbers.containsKey( 4 ); if( keyExists ) System.out.println("Key exists"); else System.out.println("Key does not exist"); } } |
Output
1 2 3 |
Key exists Key exists Key does not exist |
Which method to use? get vs containsKey method performance
Let’s look at the source code of both of these methods first.
1 2 3 4 |
public V get(Object key) { Node<K,V> e; return (e = getNode(hash(key), key)) == null ? null : e.value; } |
1 2 3 |
public boolean containsKey(Object key) { return getNode(hash(key), key) != null; } |
As far as performance is concerned, there is not much difference between these two methods. However, the suggested way to check if the key exists in the HashMap is to use the containsKey
method because of these two reasons.
1. If the map contains the specified key mapped to a null value, the get
method approach will give a false result. The containsKey
method returns true even if the key is mapped to a null value.
2. Usage of containsKey
method makes code more readable as it clearly states the purpose of the code.
How to check if a key exists if the key is an object of a custom class?
Let’s have a look at below given example in which map keys are objects of the custom class Empl.
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; class Empl{ private Integer id; private String name; public Empl(Integer id, String name){ this.id = id; this.name = name; } } public class HashMapContainsKeyExample { public static void main(String[] args) { HashMap<Empl, Integer> hMapEmployees = new HashMap<Empl, Integer>(); //add some employee objects hMapEmployees.put( new Empl(1, "John"), 1 ); hMapEmployees.put( new Empl(2, "Emma"), 2 ); hMapEmployees.put( new Empl(3, "Jessica"), 3 ); System.out.println( hMapEmployees.containsKey( new Empl(2, "Emma") ) ); } } |
Output
1 |
false |
Even though the map contained the key mapped to a value, the containsKey
method returned false. It is because our Empl class has not overridden the equals
and hashCode
methods. Any custom class whose object is used as the key of the map needs to override these two methods for the containsKey
method to work correctly.
Let’s override these two methods in our Empl class and see if the containsKey
method works as expected.
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import java.util.HashMap; class Empl{ private Integer id; private String name; public Empl(Integer id, String name){ this.id = id; this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Empl other = (Empl) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } public class HashMapContainsKeyExample { public static void main(String[] args) { HashMap<Empl, Integer> hMapEmployees = new HashMap<Empl, Integer>(); //add some employee objects hMapEmployees.put( new Empl(1, "John"), 1 ); hMapEmployees.put( new Empl(2, "Emma"), 2 ); hMapEmployees.put( new Empl(3, "Jessica"), 3 ); System.out.println( hMapEmployees.containsKey( new Empl(2, "Emma") ) ); } } |
Output
1 |
true |
This example is part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.