.containsValue()

itskarelleh's avatar
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? - true
Flowers that have a quantity of 3 is present? - false

All contributors

Contribute to Docs

Learn Java on Codecademy