Java TreeMap values() Method

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

Definition

In Java, values() returns a Collection view of the values in a TreeMap. The values come out in the order of their keys, not sorted by value, and duplicates are kept.

It is a view: it reflects later changes to the map, and removing a value through it removes the whole mapping. It suits totals, searches, and bulk removal by value.

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

Loop over the values or remove by value.

Java
TreeMap<String, Integer> expenses = new TreeMap<>();
int total = 0;
for (int amount : expenses.values()) {
    total += amount;
}
CallWhat it does
map.values()Live view of the values, in key order.
map.values().remove(v)Removes the first mapping (by key order) with value v.
map.values().removeIf(p)Removes every mapping whose value matches.

Values are not sorted by size. To get the largest value, use Collections.max(map.values()); to sort them, copy into a list first.

Library declarations
Java
// java.util.TreeMap<K, V>
public Collection<V> values()

// Declared in Map<K, V> and SortedMap<K, V>

Parameters

ParameterDescription
(none)values takes no parameters.

Return Value

Return ValueDescription
Collection<V>A live view of the values, iterated in ascending key order.

Compare with Similar Methods

Views of a TreeMap compared:

MethodHow it differs
keySet()The keys instead of the values.
entrySet()Keys and values, with setValue.
containsValue(v)Checks for a value, O(n).
Collections.frequency()Counts how often a value occurs.

Java Examples

Example 1 — Add Up Monthly Expenses

Categories are keys; the values view feeds a total and a maximum.

Java
import java.util.Collections;
import java.util.TreeMap;

public class TreeMapValuesSum {
    public static void main(String[] args) {
        TreeMap<String, Integer> expenses = new TreeMap<>();
        expenses.put("rent", 900);
        expenses.put("food", 320);
        expenses.put("bus", 60);
        expenses.put("gym", 35);

        int total = 0;
        for (int amount : expenses.values()) {
            total += amount;
        }
        System.out.println("values in key order: " + expenses.values());
        System.out.println("total: " + total);
        System.out.println("largest: " + Collections.max(expenses.values()));
    }
}

How It Works

  1. Ordered by category name. The values follow the keys bus, food, gym, rent, so they print as 60, 320, 35, 900, which is not sorted by amount.
  2. Unboxing in the loop. Each Integer is unboxed into amount and added up to 1315.
  3. Any Collection helper works. Collections.max accepts the view directly and finds 900, the rent.

Example 2 — values().remove() Takes One, removeIf() Takes All

Seat statuses: one call frees the first match only; the other clears every match.

Java
import java.util.TreeMap;

public class TreeMapValuesRemove {
    public static void main(String[] args) {
        TreeMap<String, String> seats = new TreeMap<>();
        seats.put("A1", "free");
        seats.put("A2", "sold");
        seats.put("B1", "free");
        seats.put("B2", "free");

        seats.values().remove("free");
        System.out.println("after remove: " + seats);
        seats.values().removeIf(s -> s.equals("free"));
        System.out.println("after removeIf: " + seats);
        System.out.println("still has free: " + seats.containsValue("free"));
    }
}

How It Works

  1. First match in key order. remove("free") walks the values from A1 onward and deletes only the A1 mapping.
  2. Every match. removeIf visits all remaining values and drops B1 and B2, leaving the sold seat.
  3. Confirmed by a value search. containsValue("free") now returns false.

Example 3 — A Live View Next to an ArrayList Copy

The view tracks the map; a copied list keeps the old values and is the one you can add to.

Java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeMap;

public class TreeMapValuesLiveCopy {
    public static void main(String[] args) {
        TreeMap<Integer, String> tasks = new TreeMap<>();
        tasks.put(1, "wash");
        tasks.put(2, "cook");

        Collection<String> live = tasks.values();
        List<String> copy = new ArrayList<>(tasks.values());
        tasks.put(3, "shop");
        tasks.put(1, "dry");

        System.out.println("live view: " + live);
        System.out.println("copy: " + copy);
        try {
            live.add("rest");
        } catch (UnsupportedOperationException e) {
            System.out.println("live.add: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. The view sees every update. Both the new task 3 and the replaced value for key 1 appear in live.
  2. The copy is frozen in time. The ArrayList was filled before the changes, so it still says wash and cook.
  3. Only the copy is growable. add on the view throws UnsupportedOperationException, while copy.add("rest") would work.

Frequently Asked Questions

In the order of their keys (ascending by the map's ordering), not sorted by the values themselves.
No. It is a live view of the map's values; use new ArrayList<>(map.values()) for a copy.
No. add() throws UnsupportedOperationException because a value needs a key.
Call map.values().removeIf(v -> v.equals(target)). values().remove(target) removes only the first match.
Loop over values() and add them up, or use map.values().stream().mapToInt(Integer::intValue).sum() in Java 8 and later.

Back to the method index

See all 45 TreeMap method guides grouped by what they do.

Method index →

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.

15 people found this page helpful