In Java, values() returns a Collection<V> holding every value of a HashMap. It is a view: changes to the map show up in it, and removing a value from it removes the matching mapping from the map.
It is a Collection rather than a Set because different keys can map to equal values, so duplicates are normal. The values come out in the same internal hash order as keySet().
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
No arguments; returns a Collection<V> view.
Java
Map<String, Integer> expenses = new HashMap<>();
expenses.put("rent", 900);
expenses.put("food", 300);
int total = 0;
for (int amount : expenses.values()) {
total += amount; // 1200
}
Call
What it does
for (V v : map.values())
Loop over every value.
map.values().removeIf(test)
Removes every mapping whose value passes the test.
map.values().contains(v)
Same as map.containsValue(v).
new ArrayList<>(map.values())
An independent copy, for sorting or indexing.
The view has no keys, so you cannot tell which key a value belongs to while looping. Use entrySet() when you need both.
Library declarations
Java
// java.util.HashMap<K, V>
public Collection<V> values()
// Declared in Map<K, V>
Parameters
Parameter
Description
None
values() takes no parameters.
Return Value
Return Value
Description
Collection<V>
A live view of the values. Supports removal (remove, removeIf, removeAll, retainAll, clear) but not add.
Changes every value; the view itself cannot set values.
Java Examples
Example 1 — Total and Largest Monthly Expense
Add up the values, print the view, and find the largest value with Collections.max.
Java
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class HashMapValuesDemo {
public static void main(String[] args) {
Map<String, Integer> expenses = new HashMap<>();
expenses.put("rent", 900);
expenses.put("food", 300);
expenses.put("bus", 60);
int total = 0;
for (int amount : expenses.values()) {
total += amount;
}
System.out.println("values: " + expenses.values());
System.out.println("total: " + total);
System.out.println("highest: " + Collections.max(expenses.values()));
}
}
Output
values: [60, 900, 300]
total: 1260
highest: 900
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Values only. The enhanced for loop receives each amount and unboxes it into an int; the keys are not needed for a total.
Order of the view. The printed values follow the hash order of their keys (bus, rent, food), not the order of the put calls.
Order does not matter here. 900 + 300 + 60 is 1260 whatever order the loop visits them in.
Works with Collections. The view is an ordinary Collection, so Collections.max can find the largest value, 900.
Example 2 — Duplicate Values and How to Count Them
Four people, three cities: count the values, the distinct values, and one city's frequency.
HashMap iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Condition on values.removeIf(qty -> qty == 0) unboxes each quantity and tests it; the keys are never examined.
Mappings disappear. Removing a value from the view removes its whole entry, so bolts and screws are gone from inventory.
Did anything change?removeIf returns true because at least one element was removed.
Read and remove only. A value cannot be added without a key, so values().add(5) throws UnsupportedOperationException.
Frequently Asked Questions
Call map.values(). It returns a Collection view that you can loop over or pass to methods such as Collections.max.
Different keys can map to equal values, so the values may contain duplicates.
Yes. Removing a value from the view removes its mapping from the map. removeIf removes every mapping whose value matches.
No. add() throws UnsupportedOperationException because a value needs a key.
Copy them: List<Integer> list = new ArrayList<>(map.values());