In Java, containsValue(value) returns true if one or more keys in a HashMap are mapped to value. Values are compared with equals(), and null is allowed.
A hash map is organized by keys, not values, so this method has to walk through every entry. Its running time grows with the size of the map, unlike containsKey(), which jumps straight to one bucket.
An instance method of java.util.HashMap (class since Java 1.2): a hash table of key/value pairs with no guaranteed order, one null key, any number of null values, and no thread safety.
Syntax
Pass a value; you get a boolean.
Java
Map<String, Integer> scores = new HashMap<>();
scores.put("Ann", 90);
boolean a = scores.containsValue(90); // true
boolean b = scores.containsValue(100); // false
Call
What it does
map.containsValue(v)
true if any key maps to v.
map.containsValue(null)
true if any key maps to null.
map.values().contains(v)
Same answer through the values view.
map.containsKey(k)
The fast check, for keys.
containsValue only says whether a value exists. To learn which keys hold it, loop over entrySet().
Library declarations
Java
// java.util.HashMap<K, V>
public boolean containsValue(Object value)
// Declared in Map<K, V>
Parameters
Parameter
Description
value (Object)
The value to search for. May be null.
Return Value
Return Value
Description
boolean
true if at least one mapping has an equal value; otherwise false.
Duplicates are fine. Two students scored 90; the scan stops at the first match it meets and returns true.
Full scan on a miss. No one scored 100, so every entry is checked before the method gives up and returns false.
Names are keys. "Raj" is a key, so containsKey finds it.
Wrong side of the map. No value equals the string "Raj" (the values are numbers), so containsValue returns false; it accepts any Object, so this compiles.
Example 2 — Values Are Compared with equals(), Including null
Search with a separate String object, for a null value, and with different capitalization.
Java
import java.util.HashMap;
import java.util.Map;
public class HashMapContainsValueEquals {
public static void main(String[] args) {
Map<String, String> favorites = new HashMap<>();
favorites.put("Ivy", "blue");
favorites.put("Jon", null);
String typed = new String("blue");
System.out.println("same object? " + (typed == favorites.get("Ivy")));
System.out.println("containsValue(typed): " + favorites.containsValue(typed));
System.out.println("containsValue(null): " + favorites.containsValue(null));
System.out.println("containsValue(\"Blue\"): " + favorites.containsValue("Blue"));
}
}
Output
same object? false
containsValue(typed): true
containsValue(null): true
containsValue("Blue"): false
How It Works
Two objects.new String("blue") creates a new object, so == against the stored value is false.
equals decides.containsValue compares with equals(), and the two strings have the same characters, so the result is true.
null is searchable. Jon's value is null, and containsValue(null) finds it without throwing.
Exact characters. "Blue" with a capital B is not equal to "blue", so that search fails.
Example 3 — Find Every Student with a Given Grade
Check that grade A exists, collect the names that have it from entrySet(), and sort them.
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HashMapContainsValueReverse {
public static void main(String[] args) {
Map<String, String> grades = new HashMap<>();
grades.put("Noah", "A");
grades.put("Emma", "B");
grades.put("Liam", "A");
grades.put("Ava", "C");
String wanted = "A";
if (grades.containsValue(wanted)) {
List<String> names = new ArrayList<>();
for (Map.Entry<String, String> e : grades.entrySet()) {
if (e.getValue().equals(wanted)) {
names.add(e.getKey());
}
}
Collections.sort(names);
System.out.println("grade " + wanted + ": " + names);
}
System.out.println("grade F present? " + grades.containsValue("F"));
}
}
Output
grade A: [Liam, Noah]
grade F present? false
How It Works
Cheap test first.containsValue("A") confirms at least one A exists before building a list.
Reverse lookup. A map only finds values by key, so the loop walks entrySet() and keeps each key whose value equals "A".
Predictable order. Entries come out in hash order, so Collections.sort puts the names in alphabetical order before printing.
A miss. Nobody has an F, so the second search scans every entry and returns false.
Frequently Asked Questions
Call map.containsValue(value). It returns true if any key is mapped to an equal value.
Values are not hashed, so the map must check every entry. containsKey() only checks one bucket.
Yes. It returns true if at least one key is mapped to null.
Loop over entrySet() and collect the keys whose getValue() equals the target. Several keys can share one value.
equals(). Two different objects with equal content count as a match.