Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the collection that HashMap.values() method in Java returns?

Tags:

java

hashmap

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.

like image 916
Krishnan Ravikumar Avatar asked Jun 18 '13 14:06

Krishnan Ravikumar


People also ask

What does values () return in Java HashMap?

The Java HashMap values() method returns a view of all the values present in entries of the hashmap.

What is the return type of values () method in map?

The values() method returns a new iterator object that contains the values for each element in the Map object in insertion order.

What does a HashMap return?

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.

What is HashMap collection in Java?

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.


1 Answers

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.

like image 120
Arnaud Denoyelle Avatar answered Oct 20 '22 17:10

Arnaud Denoyelle