✅ Using HashMap and ArrayList to Store Test Data in Java
You can combine HashMap and ArrayList in several ways depending on your test design:
1️⃣ A List of Maps (Most Common for Data-Driven Testing)
Each test dataset is a Map, and you store multiple Maps in an ArrayList.
Example Structure
ArrayList<HashMap<String, String>>
✔ Use Case
You have several user records (username, password, role, etc.) and want to iterate through them as separate datasets.
๐น Example: Storing Multiple Users
import java.util.ArrayList;
import java.util.HashMap;
public class TestDataExample {
public static ArrayList<HashMap<String, String>> getLoginData() {
ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
HashMap<String, String> user1 = new HashMap<>();
user1.put("username", "john123");
user1.put("password", "pass123");
user1.put("role", "admin");
HashMap<String, String> user2 = new HashMap<>();
user2.put("username", "mary007");
user2.put("password", "pass789");
user2.put("role", "user");
dataList.add(user1);
dataList.add(user2);
return dataList;
}
public static void main(String[] args) {
for (HashMap<String, String> user : getLoginData()) {
System.out.println("User Data: " + user);
System.out.println("Username: " + user.get("username"));
System.out.println("Password: " + user.get("password"));
}
}
}
Output
User Data: {username=john123, password=pass123, role=admin}
User Data: {username=mary007, password=pass789, role=user}
Perfect for plugging into TestNG @DataProvider.
๐น Example: Using This Data in TestNG
@DataProvider(name = "loginData")
public Object[][] loginData() {
ArrayList<HashMap<String, String>> data = TestDataExample.getLoginData();
return new Object[][] {
{ data.get(0) },
{ data.get(1) }
};
}
@Test(dataProvider = "loginData")
public void testLogin(HashMap<String, String> userData) {
System.out.println("Testing login: " + userData.get("username"));
}
2️⃣ A HashMap of Lists (When One Key Has Multiple Values)
Example Structure
HashMap<String, ArrayList<String>>
✔ Use Case
Storing grouped test values like:
multiple email IDs
lists of invalid inputs
dropdown menu options
๐น Example: Storing Invalid Inputs
import java.util.ArrayList;
import java.util.HashMap;
public class DataBank {
public static HashMap<String, ArrayList<String>> getInvalidData() {
HashMap<String, ArrayList<String>> invalidData = new HashMap<>();
ArrayList<String> badEmails = new ArrayList<>();
badEmails.add("abc");
badEmails.add("test@");
badEmails.add("@domain.com");
ArrayList<String> badPhones = new ArrayList<>();
badPhones.add("1234");
badPhones.add("999999999999");
badPhones.add("phone123");
invalidData.put("emails", badEmails);
invalidData.put("phones", badPhones);
return invalidData;
}
public static void main(String[] args) {
HashMap<String, ArrayList<String>> data = getInvalidData();
System.out.println(data.get("emails"));
System.out.println(data.get("phones"));
}
}
3️⃣ A Map Inside a Map (Nested Structure)
Example Structure
HashMap<String, HashMap<String, String>>
✔ Use Case
Grouping large structured test sets like:
form fields
product details
test environment configs
๐น Example: Environment Configuration
HashMap<String, HashMap<String, String>> env = new HashMap<>();
HashMap<String, String> dev = new HashMap<>();
dev.put("url", "https://dev.example.com");
dev.put("db", "dev_db");
HashMap<String, String> qa = new HashMap<>();
qa.put("url", "https://qa.example.com");
qa.put("db", "qa_db");
env.put("DEV", dev);
env.put("QA", qa);
System.out.println(env.get("QA").get("url")); // Prints QA URL
4️⃣ ArrayList of Custom Objects (Cleaner Alternative)
Sometimes instead of maps, it’s cleaner to use a POJO class.
Example
class User {
String username;
String password;
String role;
public User(String username, String password, String role) {
this.username = username;
this.password = password;
this.role = role;
}
}
ArrayList<User> users = new ArrayList<>();
users.add(new User("john", "123", "admin"));
✔ Best for large, structured data
✔ More readable than maps
✔ Works beautifully with TestNG
๐ฏ When to Use What?
Use Case Data Structure Best For
Multiple test records ArrayList<HashMap<...>> Data-driven tests
One key → many values HashMap<String, ArrayList<...>> Invalid data, dropdown items
Nested structured data HashMap<String, HashMap<...>> Environments, configs
Clean, typed objects ArrayList<POJO> Large structured test data
Learn Selenium with JAVA Training in Hyderabad
Read More
Parameterization in TestNG Using @DataProvider
Working with CSV Files in Java for Test Automation
Using JSON for Test Data in Java Selenium
Writing Test Results into Excel Files in Selenium
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments