Input
Output
import java.util.HashMap; import java.util.Map; public class HashMapExample { public static void main(String[] args) { HashMap<Integer, String> employeeMap = new HashMap<>(); // Adding key-value pairs employeeMap.put(1, "Alice"); employeeMap.put(2, "Bob"); employeeMap.put(3, "Charlie"); // Access and modify System.out.println("Employee 2: " + employeeMap.get(2)); employeeMap.put(2, "Bobby"); // Iterating using entrySet for (Map.Entry<Integer, String> entry : employeeMap.entrySet()) { System.out.println(entry.getKey() + " -> " + entry.getValue()); } // Removing an entry employeeMap.remove(3); // Check contents System.out.println("Has key 1? " + employeeMap.containsKey(1)); System.out.println("Has value 'Charlie'? " + employeeMap.containsValue("Charlie")); // Clear the map employeeMap.clear(); System.out.println("Is map empty? " + employeeMap.isEmpty()); } }