Java HashMap containsValue() method example

HQ ¬Ľ Java Tutorial ¬Ľ java.util ¬Ľ Hashmap ¬Ľ containsValue() method example
Java HashMap containsValue() method example 2018-02-16T07:27:19+00:00

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 :

[table] DataType|Parameter|Description
Object|value|value whose presence in this map is to be tested[/table]

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 :


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.

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 :

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.