1. Quick Answer

A simple way to remember the three collection types is:

  • Many values → List
  • Unique values → Set
  • Key–Value data → Map

This memory formula summarizes the primary purpose of each collection type and is commonly used to explain the differences during Java and Selenium interviews.


2. Comparison at a Glance

Aspect List Set Map
Order Maintained (Index-based) Generally not guaranteed Generally not guaranteed (LinkedHashMap preserves insertion order)
Duplicates Allowed Not allowed Duplicate keys are not allowed
Access Method Index Iteration Key
Data Structure Ordered collection of values Collection of unique values Key → Value pairs
Common Implementations ArrayList, LinkedList HashSet, LinkedHashSet HashMap, LinkedHashMap
Automation Usage Web elements, test data Window handles Test data, configuration

3. List — Ordered, Duplicates Allowed

A List is used when you need to store multiple values while preserving their order.

Advertisement

Characteristics

  • Order is maintained.
  • Duplicate values are allowed.
  • Elements are accessed using indexes.
  • Suitable for storing sequential data.

Selenium Example

List<WebElement> buttons = driver.findElements(By.tagName("button"));

findElements() returns a List because multiple matching elements may exist on a web page.

Common Implementations

  • ArrayList – Efficient random access using indexes.
  • LinkedList – Efficient insertion and deletion operations.

The detailed comparison between ArrayList and LinkedList is covered in your Java Collections tutorials.


4. Set — Unique Values Only

A Set stores only unique values.

Duplicate elements are automatically ignored.

Characteristics

  • No duplicate values.
  • Typically unordered.
  • Suitable for storing unique objects.

Selenium Example

Set<String> windowHandles = driver.getWindowHandles();

Selenium returns a Set because browser window handles are always unique.

Common Implementations

  • HashSet
  • LinkedHashSet

LinkedHashSet preserves insertion order while still preventing duplicates.


5. Map — Key–Value Pairs

A Map stores data using key-value pairs.

This structure is ideal whenever data needs to be retrieved using a meaningful key.

Selenium Example

Map<String, String> loginData = new HashMap<>();
loginData.put("username", "admin");
loginData.put("password", "admin123");

driver.findElement(By.id("user")).sendKeys(loginData.get("username"));

Maps are commonly used for:

  • Test data
  • Configuration values
  • Environment settings
  • API response parsing

Common Implementations

  • HashMap
  • LinkedHashMap

Advanced topics such as HashMap internals, hashing, buckets, synchronized maps, and ConcurrentHashMap are covered in your Java Collections tutorials.


6. Real Automation Examples

The following table summarizes where each collection is commonly used within Selenium automation frameworks.

Automation Scenario Recommended Collection
Multiple Web Elements List
Browser Window Handles Set
Test Data Map
API Response Parsing List / Map
Failed Test Collection List

Data-Driven Framework Pattern

One of the most common framework structures is:

List<Map<String, String>>

This represents:

  • A List containing multiple test records.
  • Each test record is stored as a Map of field → value pairs.

This pattern is widely used in data-driven automation frameworks.


7. Which Collection Should You Use?

Choose the collection based on your requirement.

Requirement Recommended Collection
Multiple ordered values List
Unique values only Set
Key-value lookup Map
Multiple records with multiple fields List<Map<String, String>>

For complete explanations and implementation details, refer to the Complete Java for Testers Guide and the Complete Automation Framework Guide.


Frequently Asked Questions

What is the difference between List, Set, and Map?

  • List maintains order and allows duplicates.
  • Set stores only unique values.
  • Map stores key-value pairs.

A simple memory formula is:

  • Many values → List
  • Unique values → Set
  • Key-value data → Map

Why does Selenium use a Set for window handles?

Window handle IDs are always unique.

Because duplicates are not possible, Selenium's getWindowHandles() method returns a Set<String>.


What is the difference between HashSet and HashMap?

  • HashSet stores unique values.
  • HashMap stores key-value pairs with unique keys.

Where is Map commonly used in an automation framework?

Maps are commonly used for:

  • Test data
  • Configuration values
  • Environment variables
  • API responses

Examples include:

  • loginData.get("username")
  • Browser configuration
  • Environment configuration

What is List<Map<String, String>> used for?

It is a common data-driven framework structure.

Each Map represents one test record, while the List stores multiple records.