java.util.HashMap containsValue(Object value)

Description :

On this document we will be showing a java example on how to use the containsValue() method of HashMap Class. Basically this method is to check if the HashMap contains the Object value specified as method argument. This method returns true if one or more keys were found that contains the specified value, otherwise return false. This would be helpful on scenario wherein we have to check the existence of a specific name already only the map so as to avoid duplication.

From the discussion above what would be the Object type of value to be specified as argument, would it be any type? That would be depending on how we instantiated our HashMap. Please take a look below on how to instantiate a HashMap object:

HashMap<Integer,String> map = new HashMap<Integer,String>();

Looking at the above code snippet, the key would be Integer while the value is String. So it means that we need to pass an String object or its subclass as the value on the containsValue() method.

Important notes for containsValue(Object value) method:

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

Method Syntax :

public boolean containsValue(Object value)

Parameter Input :

 

DataType Parameter Description
Object value value whose presence in this map is to be tested

 

Method Returns :

The containsValue() method returns true if this map contains one or more keys to the specified value.

Compatibility Version :

Requires Java 1.2 and up

Exception :

N/A

Java Code Example :

Below is a java code demonstrates the use of containsValue() method of HashMap class.  Let’s discuss one by one on the code logic that we have used on the example. First we have declared a HashMap with String as key and String as value HashMap<Integer, String> mapStudent = new HashMap<Integer, String>(); . So basically we have a key value mapping of student name with their corresponding id as keys. We have used the scanner object to get the console input for student name. The student were then check to the HashMap using the method containsValue(). If the containsValue method return true then it means that the student is in the HashMap thus we have printed a message that the Student were found on the database. If the return value of the containsValue(Object value) method were false it means that the input on the console were not on the HashMap thus a message of Student not found will be printed on the console if this is the case.

package com.javatutorialhq.java.examples;

import java.util.HashMap;
import java.util.Scanner;

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

public class HashMapContainsValueExample {

	public static void main(String[] args) throws InterruptedException {

		// 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 user input of student name
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter Student Name:");
		String name = scan.nextLine();
		
		// check if the student name were in student HashMap
		if(mapStudent.containsValue(name)){
			System.out.println("Student "+name +" found on the database");			
		}
		else{
			System.out.println("Student "+name +" not found");
		}
		
		// close the scanner object to avoid memory leak
		scan.close();			

	}
}

Sample Output :

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

HashMap containsValue example output

HashMap containsValue example output

Similar Method :

The following are the list of method that you can go through for better understanding of the presented example

  • HashMap put()
  • Scanner nextLine()
  • Scanner close()

Suggested Reading List :

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

References :