1. Introduction

The HashMap class in Java is part of the Java Collections Framework and is used to store key-value pairs. The put() method is used to insert a new key-value pair into the map, and the get() method is used to retrieve the value associated with a particular key.

2. Program Steps

1. Create a HashMap object.

2. Use the put() method to add key-value pairs to the HashMap.

3. Print the entire HashMap.

4. Use the get() method to retrieve the value associated with a specific key.

5. Print the retrieved value.

3. Code Program

import java.util.HashMap;

public class HashMapPutAndGet {

    public static void main(String[] args) {

        // Step 1: Create a HashMap object
        HashMap<String, Integer> map = new HashMap<>();

        // Step 2: Use the put() method to add key-value pairs to the HashMap
        map.put("Apple", 10);
        map.put("Banana", 20);
        map.put("Cherry", 30);

        // Step 3: Print the entire HashMap
        System.out.println("HashMap: " + map);

        // Step 4: Use the get() method to retrieve the value associated with a specific key
        Integer value = map.get("Banana");

        // Step 5: Print the retrieved value
        System.out.println("Value associated with key &apos;Banana&apos;: " + value);
    }
}

Output:

HashMap: {Banana=20, Apple=10, Cherry=30}
Value associated with key 'Banana': 20

4. Step By Step Explanation

Step 1: A HashMap object named map is created to store String keys and Integer values.

Step 2: The put() method is used to add three key-value pairs to the HashMap. These are "Apple" with value 10, "Banana" with value 20, and "Cherry" with value 30.

Step 3: The entire HashMap is printed, showing all the key-value pairs in it. The order of the pairs is not guaranteed to be in any specific order since HashMap does not maintain any order.

Step 4: The get() method is used to retrieve the value associated with the key "Banana". The get() method returns the value to which the specified key is mapped, or null if the map contains no mapping for the key.

Step 5: The retrieved value, 20, associated with the key "Banana", is printed.