Java HashMap containsValue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
HashMap

Definition

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
CallWhat 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

ParameterDescription
value (Object)The value to search for. May be null.

Return Value

Return ValueDescription
booleantrue if at least one mapping has an equal value; otherwise false.

Compare with Similar Methods

Searching keys and values:

MethodHow it differs
containsKey(k)Searches keys in constant time on average.
values()The collection that containsValue effectively scans.
entrySet()Loop over it to find the keys for a value.
Collections.frequencyCounts how many keys share a value.

Java Examples

Example 1 — Did Anyone Score 90?

Search the scores, then compare searching for a name as a key and as a value.

Java
import java.util.HashMap;
import java.util.Map;

public class HashMapContainsValueDemo {
    public static void main(String[] args) {
        Map<String, Integer> scores = new HashMap<>();
        scores.put("Ann", 90);
        scores.put("Raj", 75);
        scores.put("Lee", 90);

        System.out.println("scored 90? " + scores.containsValue(90));
        System.out.println("scored 100? " + scores.containsValue(100));
        System.out.println("containsKey(\"Raj\"): " + scores.containsKey("Raj"));
        System.out.println("containsValue(\"Raj\"): " + scores.containsValue("Raj"));
    }
}

How It Works

  1. Duplicates are fine. Two students scored 90; the scan stops at the first match it meets and returns true.
  2. Full scan on a miss. No one scored 100, so every entry is checked before the method gives up and returns false.
  3. Names are keys. "Raj" is a key, so containsKey finds it.
  4. 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"));
    }
}

How It Works

  1. Two objects. new String("blue") creates a new object, so == against the stored value is false.
  2. equals decides. containsValue compares with equals(), and the two strings have the same characters, so the result is true.
  3. null is searchable. Jon's value is null, and containsValue(null) finds it without throwing.
  4. 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"));
    }
}

How It Works

  1. Cheap test first. containsValue("A") confirms at least one A exists before building a list.
  2. Reverse lookup. A map only finds values by key, so the loop walks entrySet() and keeps each key whose value equals "A".
  3. Predictable order. Entries come out in hash order, so Collections.sort puts the names in alphabetical order before printing.
  4. 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.

Next: entrySet()

Get a live Set view of key/value pairs.

entrySet() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful