Home Java TreeMap Methods values() Java TreeMap values() Method 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.
TreeMap<String, Integer> expenses = new TreeMap<>();
int total = 0;
for (int amount : expenses.values()) {
total += amount;
} Call What 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.util.TreeMap<K, V>
public Collection<V> values()
// Declared in Map<K, V> and SortedMap<K, V> Parameters Parameter Description (none) values takes no parameters.
Return Value Return Value Description Collection<V>A live view of the values, iterated in ascending key order.
Compare with Similar Methods Views of a TreeMap compared:
Java Examples 1. Totals 2. Remove by value 3. View vs copy Example 1 — Add Up Monthly Expenses Categories are keys; the values view feeds a total and a maximum.
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()));
}
} values in key order: [60, 320, 35, 900]
total: 1315
largest: 900 How It Works 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.Unboxing in the loop. Each Integer is unboxed into amount and added up to 1315.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.
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"));
}
} after remove: {A2=sold, B1=free, B2=free}
after removeIf: {A2=sold}
still has free: false How It Works First match in key order. remove("free") walks the values from A1 onward and deletes only the A1 mapping.Every match. removeIf visits all remaining values and drops B1 and B2, leaving the sold seat.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.
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());
}
}
} live view: [dry, cook, shop]
copy: [wash, cook]
live.add: UnsupportedOperationException How It Works The view sees every update. Both the new task 3 and the replaced value for key 1 appear in live.The copy is frozen in time. The ArrayList was filled before the changes, so it still says wash and cook.Only the copy is growable. add on the view throws UnsupportedOperationException, while copy.add("rest") would work.Frequently Asked Questions In what order does TreeMap values() return values? In the order of their keys (ascending by the map's ordering), not sorted by the values themselves.
Is values() a copy? No. It is a live view of the map's values; use new ArrayList<>(map.values()) for a copy.
Can I add to TreeMap values()? No. add() throws UnsupportedOperationException because a value needs a key.
How do I remove all entries with a certain value? Call map.values().removeIf(v -> v.equals(target)). values().remove(target) removes only the first match.
How do I sum the values of a TreeMap? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion