Choosing
Collection Types
Learn how to pick the right Java collection for any task: Lists, Sets, and Maps each have unique strengths that make your code faster and cleaner.
What Is a Collection?
A collection is an object that groups multiple elements into a single unit. Instead of managing dozens of separate variables, you store, retrieve, and manipulate data through one structured container.
Java's Collections Framework provides three core interfaces: List, Set, and Map. Each defines a different contract for how elements are organized.
The Three Core Interfaces
The Collections Hierarchy
The Decision Rule
List: Ordered and Indexed
A List maintains elements in insertion order and allows access by integer index. Duplicates are permitted. The most common implementation is ArrayList.
names.add("Alice");
names.add("Bob");
names.add("Alice"); // duplicates OK
String first = names.get(0); // "Alice"
Set: Unique Elements Only
A Set guarantees that no two elements are equal. Adding a duplicate silently fails. HashSet is the most common implementation: fast but unordered.
tags.add("java");
tags.add("coding");
tags.add("java"); // ignored, already present
System.out.println(tags.size()); // 2
Map: Key-Value Pairs
A Map associates each unique key with a value. You look up values by their key in approximately constant time with HashMap.
scores.put("Alice", 95);
scores.put("Bob", 88);
int s = scores.get("Alice"); // 95
List vs Set: What Changes?
How to Choose: Step by Step
Example 1: Student Roster
Task: Store a class roster where students are listed in the order they enrolled. Some students may share a first name.
Example 2: Unique Visitors
Task: Track which usernames have visited a webpage today. If a user visits twice, they should only be counted once.
Example 3: Phone Book
Task: Build a phone book that maps each person's name to their phone number. Given a name, retrieve the number instantly.
phoneBook.put("Alice", "555-1234");
String num = phoneBook.get("Alice"); // "555-1234"
Common Mistakes
Summary Comparison
| Feature | ArrayList | HashSet | HashMap |
|---|---|---|---|
| Duplicates | ✓ Yes | ✕ No | Unique keys |
| Ordered | ✓ Insertion | ✕ No | ✕ No |
| Index access | ✓ .get(i) | ✕ No | ✕ No |
| Key-value | ✕ No | ✕ No | ✓ .get(key) |
| Best for | Sequences | Membership | Lookups |
Question 1 of 5
You need to store a shopping cart where a customer can add the same item multiple times and the order they added items matters. Which collection is most appropriate?
Question 2 of 5
A program needs to check whether a username has already been taken. It stores all existing usernames. Which collection is the best fit?
Question 3 of 5
A teacher wants to store each student's name along with their final grade, so she can look up any student's grade by name. Which collection should she use?
Question 4 of 5
Consider this code:
s.add("X");
s.add("Y");
s.add("X");
System.out.println(s.size());
What is printed?
Question 5 of 5
A game stores the top 10 high scores in order from highest to lowest. Players can tie (same score). Which collection is most appropriate?