This example shows how to check if a value exists in the HashMap in Java using the containsValue
method. The example also shows how to check if HashMap has value if the value is an object of a custom class.
How to check if a value exists in HashMap in Java?
The containsValue
method of the Java HashMap class returns true if the map contains the specified value mapped to any key.
1 |
public boolean containsValue(Object value) |
If the specified value is not mapped to any key in the map, the containsValue
method returns false.
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 HashMapContainsValueExample { public static void main(String[] args) { HashMap<Integer, String> hMapColors = new HashMap<Integer, String>(); //add key value mappings hMapColors.put(1, "Red"); hMapColors.put(2, "Green"); hMapColors.put(3, "Blue"); /* * To check if a value exists in HashMap, use * the containsValue method. */ //this will return true as the map has value "Green" boolean valueExists = hMapColors.containsValue("Green"); System.out.println(valueExists); //this will return false as the map does not have value "Yellow" valueExists = hMapColors.containsValue("Yellow"); System.out.println(valueExists); } } |
Output
1 2 |
true false |
How to check if HashMap has the value of a custom class?
Let’s see what happens when we use an object of a custom class as the value of the HashMap.
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.HashMap; class Rectangle{ int width; int height; public Rectangle(int width, int height){ this.width = width; this.height = height; } } public class HashMapContainsValueExample { public static void main(String[] args) { //create new HashMap HashMap<Integer, Rectangle> hMapShapes = new HashMap<Integer, Rectangle>(); //add custom Rectangle objects hMapShapes.put( 1, new Rectangle(10, 15) ); hMapShapes.put( 2, new Rectangle(20, 25) ); hMapShapes.put( 3, new Rectangle(30, 35) ); boolean valueExists = hMapShapes.containsValue( new Rectangle(20, 25) ); System.out.println( valueExists ); } } |
Output
1 |
false |
As you can see from the output, even though the map has the specified value object, the containsvalue
method returns false. It is because of the containsValue
method which relies on the equals
method.
The Rectangle class I used in the example has not implemented the equals
and hashCode
methods so it inherits these methods from the superclass Object. The Object class version of the equals
method compares the references so it returns false as the references are different.
In order for the containsValue
method to work correctly, we need to implement the equals
and hashCode
methods in our custom class 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 |
import java.util.HashMap; class Rectangle{ int width; int height; public Rectangle(int width, int height){ this.width = width; this.height = height; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + height; result = prime * result + width; return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Rectangle other = (Rectangle) obj; if (height != other.height) return false; if (width != other.width) return false; return true; } } public class HashMapContainsValueExample { public static void main(String[] args) { //create new HashMap HashMap<Integer, Rectangle> hMapShapes = new HashMap<Integer, Rectangle>(); //add custom Rectangle objects hMapShapes.put( 1, new Rectangle(10, 15) ); hMapShapes.put( 2, new Rectangle(20, 25) ); hMapShapes.put( 3, new Rectangle(30, 35) ); boolean valueExists = hMapShapes.containsValue( new Rectangle(20, 25) ); System.out.println( valueExists ); } } |
Output
1 |
true |
As you can see from the output above, the containsValue
worked as expected after implementing the equals
and hashCode
methods in the custom Rectangle class. Please also see how to check if key exists in HashMap example.
This example is part of the HashMap in Java tutorial.
Please let me know your views in the comments section below.