1. Introduction

The HashMap class in Java provides a convenient method named containsValue() to check whether a specific value is associated with a key within the map. The containsValue() method returns true if the map maps one or more keys to the specified value, otherwise false.

2. Program Steps

1. Initialize a HashMap object and populate it with some key-value pairs.

2. Utilize the containsValue() method to verify if a certain value is present in the HashMap.

3. Print out the result of the containsValue() method.

3. Code Program

import java.util.HashMap;

public class HashMapContainsValue {

    public static void main(String[] args) {

        // Step 1: Initialize a HashMap object and populate it with some key-value pairs
        HashMap<String, Integer> map = new HashMap<>();
        map.put("Apple", 10);
        map.put("Banana", 20);
        map.put("Cherry", 30);

        // Step 2: Utilize the containsValue() method to verify if a certain value is present in the HashMap
        boolean containsValue20 = map.containsValue(20);
        boolean containsValue25 = map.containsValue(25);

        // Step 3: Print out the result of the containsValue() method
        System.out.println("Contains value 20: " + containsValue20);
        System.out.println("Contains value 25: " + containsValue25);
    }
}

Output:

Contains value 20: true
Contains value 25: false

4. Step By Step Explanation

Step 1: A HashMap object named map is initialized and populated with three key-value pairs: "Apple" with value 10, "Banana" with value 20, and "Cherry" with value 30.

Step 2: The containsValue() method is employed to check if the HashMap contains the values 20 and 25. The method returns true for the value 20 and false for the value 25, since 25 is not a value in the HashMap.

Step 3: The results, indicating that the value 20 is present in the HashMap and the value 25 is not, are printed to the console.