7. Lists, Maps & Sets for Dynamic Test Data

Collections in Automation

1. List / ArrayList: Ordered collections with duplicates. Great for lists of search results and table rows. 2. Set / HashSet: Unordered collections of unique items. Ideal for window handles and verifying unique IDs. 3. Map / HashMap: Key-value pairs. Standard for headers, query parameters, and row data.
import java.util.*;

// List of harvested UI texts:
List<String> productTitles = new ArrayList<>();
productTitles.add("Wireless Mouse");
productTitles.add("Mechanical Keyboard");

// Map for API headers:
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer eyJhbGci...");
headers.put("Content-Type", "application/json");

// Iterate with for-each:
for (Map.Entry<String, String> entry : headers.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}

Task

  • Implement filterAdminUsers:
  • 1. Accept a Map<String, String> userRoles where key is username and value is role.
  • 2. Return a List<String> containing only usernames whose role is "ADMIN".
πŸ’‘Common doubts
Did you like the problem?
Lesson 7 of 38
Java
Loading code editor…
Next