How do HashMap.values() and HashMap.keySet() return values and keys?
问题 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