Check whether a key exists in a Map in Java
This post will discuss how to check whether a given key exists in a Map in Java. The solution should check if the map contains a mapping for a key k such that Objects.equals(key, k) holds for a given key key.
1. Using containsKey() method
The containsKey() method returns true if this map contains a mapping for the specified key. You can use it as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hashMap = new HashMap<>(); hashMap.put('A', 1); hashMap.put('B', 2); hashMap.put('C', 3); char key = 'C'; boolean isKeyExists = hashMap.containsKey(key); System.out.println(isKeyExists); // true } } |
Note that if a class does not override the equals() and hashCode() methods, and if an object of such class is inserted in a Map as a key, the containsKey() method will return false. To fix this, overload the equals and hashCode methods. Also, note that the containsKey() method throws NullPointerException if the specified key is null and the map implementation does not permit null keys.
2. Using Map.keySet() method
In Java 8 and above, you can get the stream of the keys in the map using the Map.keySet() method, and check if any elements of the stream match with the specified key. This can be done using the anyMatch() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hashMap = new HashMap<>(); hashMap.put('A', 1); hashMap.put('B', 2); hashMap.put('C', 3); char key = 'C'; boolean isKeyExists = hashMap.keySet().stream().anyMatch(k -> k == key); System.out.println(isKeyExists); // true } } |
Apache Commons Collections’ CollectionUtils.containsAny() method returns true if any item in the collection matches with any of the specified items. You can use it as follows to find a value in the map:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import org.apache.commons.collections4.CollectionUtils; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hashMap = new HashMap<>(); hashMap.put('A', 1); hashMap.put('B', 2); hashMap.put('C', 3); char key = 'C'; boolean isKeyExists = CollectionUtils.containsAny(hashMap.keySet(), key); System.out.println(isKeyExists); // true } } |
3. Using get() method
If your map doesn’t contain any null value, you can do like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hashMap = new HashMap<>(); hashMap.put('A', 1); hashMap.put('B', 2); hashMap.put('C', 3); char key = 'C'; boolean isKeyExists = hashMap.get(key) != null; System.out.println(isKeyExists); // true } } |
The above code will fail if the map contains any null value. This can be handled as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) { Map<Character, Integer> hashMap = new HashMap<>(); hashMap.put('A', 1); hashMap.put('B', 2); hashMap.put('C', null); char key = 'C'; boolean isKeyExists = hashMap.get(key) != null || hashMap.containsKey(key); System.out.println(isKeyExists); // true } } |
That’s all about checking whether a given key exists in a Map in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)