Java TreeMap containsValue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.TreeMap

Definition

In Java, containsValue(value) returns true if at least one key in the TreeMap is mapped to value. Values are compared with equals (and null matches a stored null).

A TreeMap is sorted by key, not by value, so it cannot search values quickly: the method walks the entries in key order until it finds a match, which costs O(n) in the worst case.

An instance method of java.util.TreeMap, part of TreeMap since Java 1.2. A TreeMap keeps its key/value pairs sorted by key in a red-black tree, so lookups and updates take O(log n) time; it is not thread-safe.

Syntax

Pass the value to search for.

Java
TreeMap<String, String> seatOf = new TreeMap<>();
seatOf.put("12A", "Ravi");
boolean hasRavi = seatOf.containsValue("Ravi");   // true
CallWhat it does
map.containsValue(v)true if any key maps to v.
map.containsValue(null)true if some key maps to null.
map.tailMap(k).containsValue(v)Searches only the keys from k upward.

If you search by value often, keep a second map from value to key so both directions are fast.

Library declarations
Java
// java.util.TreeMap<K, V>
public boolean containsValue(Object value)

// Declared in Map<K, V>

Parameters

ParameterDescription
value (Object)The value to find. May be null.

Return Value

Return ValueDescription
trueSome entry's value equals value.
falseNo entry has that value.

Compare with Similar Methods

Searching a TreeMap:

MethodHow it differs
containsKey(k)Searches keys in O(log n).
values()A view of all values for loops and streams.
entrySet()Loop to find which key holds the value.
HashMap.containsValueAlso a linear scan.

Java Examples

Example 1 — Check Whether a Passenger Has a Seat

Seats are keys and passenger names are values, so finding a passenger means searching the values.

Java
import java.util.TreeMap;

public class TreeMapContainsValueSeats {
    public static void main(String[] args) {
        TreeMap<String, String> seatOf = new TreeMap<>();
        seatOf.put("12A", "Ravi");
        seatOf.put("12B", "Mona");
        seatOf.put("14C", "Ravi");

        System.out.println("Ravi has a seat: " + seatOf.containsValue("Ravi"));
        System.out.println("Zoe has a seat: " + seatOf.containsValue("Zoe"));
        System.out.println("Mona's seat is a key? " + seatOf.containsKey("Mona"));
    }
}

How It Works

  1. Stops at the first match. The scan starts at 12A, whose value is Ravi, so it returns true without looking at the rest.
  2. A full scan for a miss. Zoe is not a value, so every entry is checked before false comes back.
  3. Values are not keys. containsKey("Mona") is false: Mona is stored as a value, and key methods only look at seat numbers.

Example 2 — Integer, Long, and null Values

The value type has to match exactly, and null can be searched too.

Java
import java.util.TreeMap;

public class TreeMapContainsValueEquals {
    public static void main(String[] args) {
        TreeMap<String, Integer> highScores = new TreeMap<>();
        highScores.put("ace", 1500);
        highScores.put("bolt", null);

        Integer target = 1500;
        System.out.println("contains 1500: " + highScores.containsValue(target));
        System.out.println("contains 1500L: " + highScores.containsValue(1500L));
        System.out.println("contains null: " + highScores.containsValue(null));
    }
}

How It Works

  1. Same type, same value. The stored Integer 1500 equals target, so the first check is true.
  2. Long is a different type. 1500L is boxed to a Long, and Integer.equals(Long) is always false, so the search fails silently.
  3. null is searchable. bolt has a null score, and containsValue(null) finds it.

Example 3 — Search Values Within a Range of Years

Medal results by year: ask about all years, the years before 2023, and the years from 2024.

Java
import java.util.TreeMap;

public class TreeMapContainsValueRange {
    public static void main(String[] args) {
        TreeMap<Integer, String> results = new TreeMap<>();
        results.put(2022, "silver");
        results.put(2023, "gold");
        results.put(2024, "bronze");
        results.put(2025, "gold");

        System.out.println("gold ever: " + results.containsValue("gold"));
        System.out.println("gold before 2023: " + results.headMap(2023).containsValue("gold"));
        System.out.println("gold since 2024: " + results.tailMap(2024).containsValue("gold"));
    }
}

How It Works

  1. Whole map. 2023 holds gold, so the unrestricted search succeeds.
  2. Only 2022 is before 2023. headMap(2023) contains just 2022=silver, so no gold is found there.
  3. From 2024 on. tailMap(2024) includes 2024 and 2025, and 2025 is gold; scanning a view also reduces the work to that range.

Frequently Asked Questions

O(n). The tree is ordered by key, so values can only be found by walking the entries.
Yes. A stored value matches when value.equals(stored) is true, or when both are null.
The stored value is an Integer and 1500L is a Long. Integer.equals returns false for any non-Integer argument.
Yes. containsValue(null) returns true if any key is mapped to null.
Loop over entrySet() and compare each getValue(), or keep a second map from values to keys.

Next: descendingKeySet()

Walk the keys from largest to smallest.

descendingKeySet() 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.

7 people found this page helpful