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.
The Java HashMap values() method returns a view of all the values present in entries of the hashmap.
The values() method returns a new iterator object that contains the values for each element in the Map object in insertion order.
HashMap get() Method in Java HashMap. get() method of HashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key.
The HashMap class of the Java collections framework provides the functionality of the hash table data structure. It stores elements in key/value pairs. Here, keys are unique identifiers used to associate each value on a map. The HashMap class implements the Map interface.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With