java.util.HashMap values()

Description :

On this document we will be showing a java example on how to use the values() method of HashMap Class. Basically this method is to get all the values that the HashMap contains. If we are only interested in getting the values which the HashMap contains regardless of the keys tied to the object values, then this method is suitable instead of using keySet() and get method. Well it might defeat the purpose of HashMap which is a key-value mapping however this is a fantastic way of getting the values inside the HashMap. The values() method returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. Please note the following important behavior:

  • If the map is modified while an iteration over the collection is in progress (except through the iterator’s own remove operation), the results of the iteration are undefined.
  • The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations.
  • It does not support the add or addAll operations.

In dealing with HashMap methods, as a general rule you must always watch out for Generics such that if we have declared our map like HashMap<Integer,String> map = new HashMap<Integer,String>();  then the returned values are expected to be String object type or its subclass. The values() method returns a Collection. The Collection class as of java 5 is already using Generics too. Hence the type parameters of our Collection should also be a String object type. Here is an example Collection<String> set = map.values()  to further understand the concept. If you are still confused, please go through the concept of generics from the official documentation. Important notes for values() method:

  • specified by values in interface Map<K,V>
  • overrides values in class AbstractMap<K,V>

Method Syntax :

public Collection<V> values()

Parameter Input :

 

DataType Parameter Description
N/A N/A N/A

 

Method Returns :

The values() method returns a view of the values contained in this map.

Compatibility Version :

Requires Java 1.2 and up

Exception :

N/A

Java Code Example :

Below is a java code demonstrates the use of values() method of HashMap class.

package com.javatutorialhq.java.examples;


import java.util.Collection;
import java.util.HashMap;


/*
 * This example source code demonstrates the use of  
 * values() method of HashMap class
 */

public class HashMapValuesExample {

	public static void main(String[] args){

		// initialize the hashmap object with Integer as key and String as value
		HashMap<Integer, String> mapStudent = new HashMap<Integer, String>();
		
		// populate the student map
		mapStudent.put(13215, "Steve Atkins");
		mapStudent.put(17891, "Albert Travis");
		mapStudent.put(98412, "Paolo Quintos");
		
		//get the values
		System.out.println("****Initial Contents of Collection****");
		Collection<String> collectionValues = mapStudent.values();
		for(String s: collectionValues){
			System.out.println(s);
		}
		
		// add another key-value mapping to the hashmap
		mapStudent.put(48754, "Robert Jaworski");
		System.out.println(""
				+ "\n****New Contents of Collection*****");
		
		for(String s: collectionValues){
			System.out.println(s);
		}

	}	

}

Sample Output :

Running the above java example of the usage of HashMap values() method will give the following output:

HashMap values method example output

HashMap values method example output

There would be two output on the console produced by the example code. First, the initial contents of the collections which contains the values inside the HashMap were printed. After which a new printout of Collection contents after we added additional key-value mapping on our HashMap. At first glance this is a straightforward example but did you notice that we have not invoked the values() method after we have put additional key-val;ue mapping on our HashMap and yet the new values were added to the collectionValues variable. This is the inherent behavior of values() method when you modify the HashMap object the Collection object would also be modified. The Collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Suggested Reading List :

The following are the recommended reading materials for further understanding of HashMap

References :