Java – HashMap (Collections Framework)

HashMap: A map-based collection that stores key-value pairs. Provides constant-time complexity for basic operations.
Example:

import java.util.HashMap;
HashMap map = new HashMap<>();
map.put(1, "John");
map.put(2, "Alice");
map.put(3, "Bob");
System.out.println(map);
System.out.println("Key 2: " + map.get(2));
map.remove(1);
System.out.println(map);
for (int key : map.keySet()) {
System.out.println(key + " -> " + map.get(key));
}

No images available.