![]() |
VOOZH | about |
The values() method of the Map interface returns a Collection view of all values stored in the map. The returned collection is backed by the map, meaning any modification to the map is immediately reflected in the collection and vice versa. Also, it does not support adding elements directly.
Example:
[Java, Python]
Explanation:
map.values()
This code depicts the way values() returns a collection containing all values stored in a HashMap.
[student 4, student 3, student 6, student 1, student 2, student 5]
Explanation:
The changes made to the map are automatically reflected in the collection returned by values(). Below given code proves the same.
Before modification student 3 student 1 student 2 After modification student 3 student 1 student 2 student 7
Explanation:
Note: The returned collection does not support add() operation. Calling add() on the values collection throws UnsupportedOperationException.