This example shows how to check if the LinkedHashMap contains the specified value in Java. The example also shows how to check if the value exists in the LinkedHashMap using the containsValue method.
How to check if the LinkedHashMap contains a value in Java?
The containsValue
method of the LinkedHashMap class returns true if the specified value is mapped to any of the keys in the LinkedHashMap object.
1 |
public boolean containsValue(Object value) |
The containsValue
method returns false if the specified value is not mapped to any key in the map.
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 |
import java.util.LinkedHashMap; public class LinkedHashMapContainsValueExample { 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); /* * To check if the value exists in LinkedHashMap, use * the containsValue method */ //returns true as value "One" is mapped to 1 boolean valueExists = lhmap.containsValue("One"); System.out.println(valueExists); //returns false as value "Three" is not mapped to any key valueExists = lhmap.containsValue("Three"); System.out.println(valueExists); } } |
Output
1 2 |
true false |
How to check if the LinkedHashMap values are objects of a custom class?
If the LinkedHashMap values are objects of a custom class, then the custom class must implement the equals
method (and preferably the hashCode
method too).
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 |
import java.util.LinkedHashMap; class Emp{ private Integer id; public Emp(Integer id){ this.id = id; } public String toString(){ return "ID: " + this.id; } } public class LinkedHashMapContainsValueExample { public static void main(String[] args) { LinkedHashMap<Integer, Emp> lhmap = new LinkedHashMap<Integer, Emp>(); lhmap.put(1, new Emp(1)); lhmap.put(2, new Emp(2)); lhmap.put(3, new Emp(3)); System.out.println( lhmap.containsValue( new Emp(2) ) ); } } |
Output
1 |
false |
As you can see from the output, the containsValue
method returns false even though the value was present in the LinkedHashMap object. It is because our Emp class has not implemented the equals
method. Let’s implement it and try again.
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.LinkedHashMap; class Emp{ private Integer id; public Emp(Integer id){ this.id = id; } public String toString(){ return "ID: " + this.id; } 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; Emp other = (Emp) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } public class LinkedHashMapContainsValueExample { public static void main(String[] args) { LinkedHashMap<Integer, Emp> lhmap = new LinkedHashMap<Integer, Emp>(); lhmap.put(1, new Emp(1)); lhmap.put(2, new Emp(2)); lhmap.put(3, new Emp(3)); System.out.println( lhmap.containsValue( new Emp(2) ) ); } } |
Output
1 |
true |
Now the containsValue
method worked as expected and returned true.
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