Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
containsvalue
map
communitycreator

What is the containsValue() method of Map in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The containsValue method of the Map interface is used to check if a value passed is associated with one or more keys in the Map.

Syntax

map.containsValue(Object value)

This method will return true if the passed value is present in the Map; otherwise, it returns false.

Example

import java.util.HashMap;
class ContainsValueCheck {
public static void main(String args[] ) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("one", 1);
map.put("ONE", 1);
map.put("two", 2);
System.out.println("The map is " + map);
System.out.println("Checking map.containsValue(1) - " + map.containsValue(1));
System.out.println("Checking map.containsValue(2) - " + map.containsValue(2));
System.out.println("Checking map.containsValue(3) - " + map.containsValue(3));
}
}

In the code above, we have created a HashMapHashMap is one of the classes which implements the Map interface. and added the below entries:

  • one - 1
  • ONE - 1
  • two - 2

Then, we used the containsValue method to check if a value is present in the map.

When checking:

map.containsValue(1);

we get true, because the value 1 is present in the map.

When checking:

map.containsValue(3);

we get false, because the value 3 is not present on the map.

RELATED TAGS

java
containsvalue
map
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring