A HashMap stores data as key-value pairs. In test automation, HashMaps store test configurations, element locators, request headers, and any data that needs fast lookup by a key.
import java.util.HashMap;
public class HashMapBasics {
public static void main(String[] args) {
HashMap<String, String> testData = new HashMap<>();
// Put key-value pairs
testData.put("username", "testuser");
testData.put("password", "Test@123");
testData.put("email", "test@example.com");
// Get value by key
System.out.println(testData.get("username")); // testuser
// Check if key or value exists
System.out.println(testData.containsKey("email")); // true
System.out.println(testData.containsValue("admin")); // false
// Size
System.out.println("Entries: " + testData.size()); // 3
// Remove
testData.remove("email");
System.out.println(testData); // {password=Test@123, username=testuser}
}
}| Method | Returns | Description |
|---|---|---|
| put(key, value) | Previous value / null | Adds or updates entry |
| get(key) | Value / null | Returns value for key |
| getOrDefault(key, default) | Value / default | Returns value or default if key absent |
| containsKey(key) | boolean | True if key exists |
| containsValue(value) | boolean | True if value exists |
| remove(key) | Value / null | Removes entry by key |
| size() | int | Number of entries |
| isEmpty() | boolean | True if no entries |
| keySet() | Set<K> | All keys |
| values() | Collection<V> | All values |
| entrySet() | Set<Entry> | All key-value pairs |
import java.util.HashMap;
import java.util.Map;
public class HashMapIterate {
public static void main(String[] args) {
HashMap<String, Integer> testCounts = new HashMap<>();
testCounts.put("Login", 5);
testCounts.put("Search", 8);
testCounts.put("Checkout", 3);
// Iterate over entries (most common)
for (Map.Entry<String, Integer> entry : testCounts.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue() + " tests");
}
// Iterate over keys only
for (String module : testCounts.keySet()) {
System.out.println("Module: " + module);
}
// Iterate over values only
int total = 0;
for (int count : testCounts.values()) {
total += count;
}
System.out.println("Total tests: " + total); // 16
}
}import java.util.HashMap;
import java.util.Map;
public class CountResults {
public static void main(String[] args) {
String[] results = {"PASS", "FAIL", "PASS", "PASS", "SKIP", "FAIL", "PASS"};
HashMap<String, Integer> counts = new HashMap<>();
for (String result : results) {
counts.put(result, counts.getOrDefault(result, 0) + 1);
}
for (Map.Entry<String, Integer> entry : counts.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}PASS: 4
FAIL: 2
SKIP: 1import java.util.HashMap;
public class LocatorMap {
public static void main(String[] args) {
HashMap<String, String> loginPage = new HashMap<>();
loginPage.put("username", "#user-input");
loginPage.put("password", "#pass-input");
loginPage.put("submit", "button[type=submit]");
loginPage.put("error", ".error-message");
// Simulate test step
System.out.println("Find element: " + loginPage.get("username"));
System.out.println("Find element: " + loginPage.get("password"));
System.out.println("Click: " + loginPage.get("submit"));
// Safe access
String locator = loginPage.getOrDefault("captcha", "not defined");
System.out.println("Captcha: " + locator); // not defined
}
}HashMap does not maintain insertion order. If order matters, use LinkedHashMap. If you need sorted keys, use TreeMap.
Q: What happens when you put a duplicate key in a HashMap?
A: The new value replaces the old value. HashMap does not allow duplicate keys — each key maps to exactly one value. The put() method returns the previous value if the key existed, or null if it was a new key. Values CAN be duplicated — multiple keys can map to the same value.
Exercise 1: Create a HashMap to store 5 country-capital pairs. Print all entries, then look up a specific country and print its capital.
Exercise 2: Write a program that counts the frequency of each character in a given String using a HashMap. Input: "automation" → a:2, u:1, t:2, o:2, m:1, i:1, n:1.
Exercise 3: Given an array of words, build a HashMap where keys are word lengths and values are ArrayLists of words with that length. Example: {3=[cat, dog], 5=[hello, world]}.