The source code of HashMap.values()
is shown as follows
public Collection values() {
Collection vs = values;
return (vs !=
The Values
class implements a Collection
that is backed by the HashMap
. As mastov commented, Values
is an inner class of HashMap
, which gives it access to the members of the enclosing HashMap
instance it is associated with. Therefore it's not empty. Its size is the size of the HashMap
, and when you iterate over it, you are iterating over the Entries of the HashMap
.
When you call System.out.println(values);
, you are calling the toString
method of AbstractCollection
, which uses an Iterator
to iterate over the values and get their String
representation. The Iterator
actually iterates over the Entries of the HashMap
and returns their values.
The same goes for the Set
s returned by keySet
and entrySet
.