Wednesday, November 26, 2025

thumbnail

Java Collections Framework: List, Set, Map

 Java Collections Framework: List, Set, Map


The Java Collections Framework (JCF) provides ready-made data structures and utility classes to store, manipulate, and process groups of objects efficiently.


The three most commonly used collection types are:


List → Ordered collection (duplicates allowed)


Set → Unordered collection (duplicates NOT allowed)


Map → Key–value pairs (keys unique)


1. List Interface


A List is an ordered collection where elements are stored by index.

Duplicates are allowed.


Common Implementations


ArrayList (most used)


LinkedList


Vector (legacy)


ArrayList Example

List<String> names = new ArrayList<>();

names.add("John");

names.add("Alice");

names.add("John");  // duplicates allowed


System.out.println(names); // [John, Alice, John]


Accessing elements

String first = names.get(0); // John


Looping through a list

for (String name : names) {

    System.out.println(name);

}


When to Use List


You need ordered data


You want to access items by index


You want to store duplicates


Useful for Selenium findElements() results


Example in Selenium:

List<WebElement> links = driver.findElements(By.tagName("a"));


2. Set Interface


A Set is a collection where duplicates are NOT allowed.

Order is not guaranteed (HashSet) or sorted (TreeSet).


Common Implementations


HashSet → no order


LinkedHashSet → insertion order


TreeSet → sorted order


HashSet Example

Set<String> cities = new HashSet<>();

cities.add("Delhi");

cities.add("Mumbai");

cities.add("Delhi");  // duplicate ignored


System.out.println(cities); // [Delhi, Mumbai] (order may vary)


Checking size

System.out.println(cities.size());


When to Use Set


You need unique values only


Order does NOT matter (or only sorted order needed)


Good for removing duplicates from a list


Example in Selenium:

Set<String> uniqueTexts = new HashSet<>(textsFromWebElements);


3. Map Interface


A Map stores data as key–value pairs.

Keys are unique, values can be duplicates.


Common Implementations


HashMap → fastest, no order


LinkedHashMap → insertion order


TreeMap → sorted by key


HashMap Example

Map<String, Integer> scores = new HashMap<>();

scores.put("John", 90);

scores.put("Alice", 95);

scores.put("John", 85); // Key 'John' overwritten


System.out.println(scores);  // {John=85, Alice=95}


Accessing values

int johnScore = scores.get("John");


Looping through a Map

for (Map.Entry<String, Integer> entry : scores.entrySet()) {

    System.out.println(entry.getKey() + " : " + entry.getValue());

}


When to Use Map


You need to store paired values (key → value)


You need fast lookups by key


Useful for:


storing configuration


mapping table headers to row values in Selenium


API testing key-value results


Example in Selenium:

Map<String, String> userData = new HashMap<>();

userData.put("Username", "Admin");

userData.put("Role", "Manager");


Comparison Table

Feature List Set Map

Stores Elements Unique elements Key–value pairs

Order Yes No (unless LinkedHashSet/TreeSet) Depends on implementation

Allows duplicates Yes No Keys: No, Values: Yes

Access by index Yes No Access by key

When to Use What?

Use List when:


✔ You need ordered data

✔ Duplicates are allowed

✔ Access by index is needed


Use Set when:


✔ You need unique values

✔ No duplicates allowed


Use Map when:


✔ You want to store data as key → value

✔ Need fast access by key


Real Selenium Example Using All Three

List<WebElement> elements = driver.findElements(By.tagName("a"));

List<String> linkTexts = new ArrayList<>();


for (WebElement e : elements) {

    linkTexts.add(e.getText());

}


Set<String> uniqueLinks = new HashSet<>(linkTexts);


Map<String, String> linkMap = new HashMap<>();

for (WebElement e : elements) {

    linkMap.put(e.getText(), e.getAttribute("href"));

}


Summary


List → ordered, duplicates allowed


Set → unique elements only


Map → key–value pairs


These structures make your code more efficient and readable


Especially useful for Selenium: storing elements, filtering, mapping, comparing values

Learn Full Stack JAVA Course in Hyderabad

Read More

Java Data Types and Variables Explained

Object-Oriented Programming in Java

☕ Core Java Concepts

Using Bootstrap in Frontend Development

Visit Our Quality Thought Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive