This example shows how to check if the LinkedHashMap in Java contains a key. The example also shows how to check if the key exists in the LinkedHashMap using the get and containsKey methods.
How to check if the LinkedHashMap contains a key in Java?
1. Using the get method
The get
method of the LinkedHashMap class returns the value mapped to the specified key. If the key does not exist in the map, the get
method returns null.
You can check the return value of the get
method and compare it to a null to see if the key exists or not.
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 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); /* * The get method returns null for the * non-existent keys */ //this will return "One", the value mapped to 1 String value = lhmap.get(1); if(value == null){ System.out.println("The key does not exist"); }else{ System.out.println("The key exists"); } //this will return null as key does not exist value = lhmap.get(3); if(value == null){ System.out.println("The key does not exist"); }else{ System.out.println("The key exists"); } |
Output
1 2 |
The key exists The key does not exist |
The problem is when your LinkedHashMap contains keys that are mapped to a null value. In that case, getting the value of those keys will return null as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, null); //this will also return null, the value mapped to 3 String value = lhmap.get(3); if(value == null){ System.out.println("The key does not exist"); }else{ System.out.println("The key exists"); } |
Output
1 |
The key does not exist |
Note: This approach is not recommended since it is not reliable if the LinkedHashMap object has null values.
2. Using the containsKey method
The containsKey
of the LinkedHashMap class returns true if the map object contains the specified key.
1 |
public boolean containsKey(Object key) |
This method returns false if the specified key does not exist in the map object.
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 |
import java.util.LinkedHashMap; public class LinkedHashMapContainsKeyExample { public static void main(String[] args) { LinkedHashMap<Integer, String> lhmap = new LinkedHashMap<Integer, String>(); lhmap.put(1, "One"); lhmap.put(2, "Two"); lhmap.put(3, null); /* * Use the containsKey method of the LinkedHashMap to * check if the key exists in the map object */ //will return true boolean exists = lhmap.containsKey(1); System.out.println(exists); //will also return true exists = lhmap.containsKey(3); System.out.println(exists); //will return false as key 4 does not exist exists = lhmap.containsKey(4); System.out.println(exists); } } |
Output
1 2 3 |
true true false |
As you can see from the output, the containsKey
method returns true even if the value mapped to the specified key is null.
If the LinkedHashMap object contains objects of a custom class as the keys, the custom class must implement the equals
and hashCode
methods 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 46 47 48 49 50 51 52 53 54 |
import java.util.LinkedHashMap; class Student{ private Integer id; private String name; public Student(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + "=>" + this.name + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } public class LinkedHashMapContainsKeyExample { public static void main(String[] args) { LinkedHashMap<Student, Integer> lhmap = new LinkedHashMap<Student, Integer>(); lhmap.put(new Student(1, "John"), 1); lhmap.put(new Student(2, "Emily"), 2); lhmap.put(new Student(3, "Maria"), 3); System.out.println( lhmap.containsKey( new Student(2, "Emily") ) ); System.out.println( lhmap.containsKey( new Student(4, "Jack") ) ); } } |
Output
1 2 |
true false |
This example is a part of the LinkedHashMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashMap