11

The source code of HashMap.values() is shown as follows

public Collection<V> values() {
    Collection<V> vs = values;
    return (vs != null ? vs : (values = new Values()));
}

As you can see, when the values() method first called, it just returns a Values object. The Values object is a subclass of AbstractCollection with no constructor, and of course contains no element. But when I called the method, it returned a collection rapidly

Collection<String> values = map.values();
System.out.println(values);

That's so weird. Not only values(), but also keySet() and entrySet() method return such empty objects. So, here is my question, when and how do these methods return objects with elements we need?

0

3 Answers 3

20

It's a misconception that the Values class is "of course empty". Just because there is no method called on it and its constructor doesn't have any arguments doesn't mean that the collection is empty.

The Values class is an "inner class" (a non-static nested class) of HashMap, which means that it has an implicit reference to the HashMap object that created it. It can therefore access all elements of the HashMap, either explicitly by using the HashMap.this reference or by just accessing the members directly. Since it is an inner class, it is even allowed to access the HashMap's private members.

You can see that for example in the Values class' implementation of the size method:

public int size() {
    return size;
}

The Values class doesn't have a size member, so that size refers to the HashMap's size. It's equivalent to:

public int size() {
    return HashMap.this.size;
}

EDIT: Note that this also means that the collection you receive is not a copy, but still refers to the original HashMap contents and therefore changes when you update the HashMap:

    // Getting the entry set (not a copy!)
    Set<Entry<String, String>> entries = map.entrySet();

    // Add elements to the map afterwards
    map.put("abc", "def");

    // Check out the entries in the collection
    // (magically containing the elements added after getting the collection)
    System.out.println(entries); // "[abc=def]"
3
  • 1
    Note that this also means that the collection you receive is not a copy, but still refers to the original HashMap contents and therefore changes when you update the HashMap: Which is also clearly spelled out in the contract of Map.values(), to avoid any misunderstanding.
    – biziclop
    Jun 8, 2015 at 12:57
  • An inner class is a non-static nested class, i.e. had the class been static, it would not be an inner class, but simply a static nested class. docs.oracle.com/javase/tutorial/java/javaOO/nested.html
    – Sardtok
    Jun 8, 2015 at 13:58
  • @Sardtok: Thanks for the hint, I just edited the answer in order to use the official terminology.
    – mastov
    Jun 8, 2015 at 17:50
13

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 Sets returned by keySet and entrySet.

8
  • Maybe you should mention that the Values class is a non-static inner class that therefore has an implicit this pointer to the outer class HashMap. That's how it can reference the elements of the map, even when its constructor is called with no arguments at all.
    – mastov
    Jun 8, 2015 at 12:05
  • Thanks @Eran and @mastov. I know the iterator method can work well when we iterate Values. However, when I set a breakpoint at the Collection<String> values = map.values(); line and debug the program, the values has already been set as a collection before System.out.println(values) been called, and there is no iteration here. This is the point where I feel strange.
    – Yohn
    Jun 8, 2015 at 13:39
  • @Yohn What do you mean by the values has already been set as a collection?
    – Eran
    Jun 8, 2015 at 13:48
  • I mean that when a breakpoint is set at the Collection<String> values = map.values(); line in IDE like Eclipse, run the program as debug mode, and step over the line, the IDE will show that the variable values has been a collection filled with all values of Hashmap, rather than an object only with some references.
    – Yohn
    Jun 8, 2015 at 14:05
  • 2
    @Yohn Clicking the values variable in the Variables view just executes the toString method of that Collection and shows it's output, so again, the Iterator is used to obtain the values from the HashMap.
    – Eran
    Jun 8, 2015 at 14:34
2
Collection<V> vs = values;
return (vs != null ? vs : (values = new Values()));

These two line answer your question.

values is a internal collection (Inherited from AbstractMap). If it is not null then it will be returned.
If it is null then it will be initialized and the new one will returned.

Now I guess, the main point of your question is
when and how do these methods return objects with elements we need?

Technically valuesalways return this initialized Object. Then how we get our entry values from these object.?
Lets go a little deep:

values() actually return an object of class Values which is an Inner class of HashMap and extends AbstractCollection

private final class Values extends AbstractCollection<V> 

As you have looked into the source code. Then you will find inside the Values class

public Iterator<V> iterator() {
        return newValueIterator();
    }

this newValueIterator() does the trick.
If you go more deeper you will find newValueIterator() returns object of ValueIterator which is a subclass of HashIterator HashIterator implements basic functionality for iteration, which actually itterate over the table maintained by the HashMap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.