Java .containsValue()
Published Mar 14, 2024
Contribute to Docs
The .containsValue() method determines whether a map contains a given value. This method returns true if the map has one or more keys that match with the given value and false if the map does not contain the given value.
Syntax
map.containsValue(value);
map: The name of the map to be checked.value: The value to be searched.
Example
The below example demonstrates the use of the .containsValue() method:
import java.util.HashMap;public class FlowerInventory {public static void main(String[] args) {HashMap<String, Integer> inventory = new HashMap<String, Integer>();inventory.put("Roses", 10);inventory.put("Lillies", 5);inventory.put("Tulips", 1);System.out.println("Flowers that have a quantity of 10 is present? - " + inventory.containsValue(10));System.out.println("Flowers that have a quantity of 3 is present? - " + inventory.containsValue(0));}}
The above code returns the following output:
Flowers that have a quantity of 10 is present? - trueFlowers that have a quantity of 3 is present? - false
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours