Java Collections
What is the Collections Framework?
Java's Collections framework provides ready-made, resizable data structures for storing groups of objects, more flexible than fixed-size arrays.
Common Collection Types
- List (e.g., ArrayList): an ordered collection that allows duplicates.
- Set (e.g., HashSet): a collection with no duplicate elements.
- Map (e.g., HashMap): stores key-value pairs for fast lookup by key.
Why Testers Use Collections
Collections are used constantly in test automation — for example, storing a List of expected values, or a Map representing a JSON request payload before sending it via Rest Assured.
Choosing the Right Collection for the Job
Picking the wrong collection is a quiet source of slow or buggy tests. Match the structure to what you actually need:
| Need | Use | Why |
|---|---|---|
| Ordered list, duplicates allowed, index access | ArrayList | Fast reads by position |
| No duplicates, fast membership check | HashSet | Near constant-time contains |
| Key to value lookup | HashMap | Fast get by key |
| Keys or elements kept in sorted order | TreeMap / TreeSet | Automatic ordering |
For example, checking that a response contains an expected id is far cleaner with a Set than scanning a List every time.
How Iteration and Ordering Differ
These collections do not all preserve order. An ArrayList keeps insertion order. A HashSet and HashMap give no ordering guarantee, so a test that assumes a fixed iteration order will be flaky. If you need predictable order, use LinkedHashMap or LinkedHashSet for insertion order, or the Tree variants for sorted order. Writing a test that passes locally but fails elsewhere is often down to relying on unordered iteration.
Collections in Automated Test Code
In test automation these structures appear constantly — holding expected results, building request data, and comparing responses.
Map<String, Object> payload = new HashMap<>();
payload.put("name", "Asha");
payload.put("role", "tester");
// send payload as a JSON body, then assert on the response
List<String> expected = List.of("red", "green", "blue");
assertEquals(expected, actualColours);
- Compare a
Listwhen order matters, aSetwhen it does not — comparing lists for data that is really unordered causes false failures. - Use
Mapto represent and assert on key-value response fields. - Prefer immutable collections for fixed expected values so a test cannot accidentally mutate them.
Two habits keep collection-heavy test code safe. First, lean on generics — declaring List<String> rather than a raw List lets the compiler catch a wrong type before the test even runs, instead of failing with a confusing cast error later. Second, be careful storing custom objects in a HashSet or as HashMap keys: unless the class defines equals and hashCode properly, two objects you consider equal will be treated as different, and a lookup that should succeed will quietly miss.
Takeaway: choose the collection that matches your access pattern, stay aware of which ones guarantee order, and lean on List, Set and Map deliberately in test code so your assertions are both fast and reliable.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.