Reputation: 295
Java SE 6.0 API says values()
method in java.util.HashMap
returns a Collection
type. How does JVM decide which collection to return at run time. Is it jvm specific or are there any general guidelines that Java follows. I browsed the source-code of HashMap
but couldn't get a clue. Any help is highly appreciated, or if the question is lame, kindly point me why.Thanks.
Upvotes: 11
Views: 4699
Reputation: 31235
You can see in the sources :
public Collection<V> values() {
if (values == null) {
values = new AbstractCollection<V>() {
...
They actually give a custom implementation of AbstractCollection
.
An important thing to know about this collection is that it is NOT Serializable : never try to send it as-is between client-server.
Please note that this extract comes from the Sun JDK sources. It means that it is specific to the vendor implementation.
Upvotes: 11
Reputation: 136042
It's not JVM who decides which collection to return at runtime but the actual implementation of Map interface. In case of HashMap this is HashMap.Values inner class, see HashMap src
Upvotes: 3