01 / 20

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.

AP Computer Science A

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

List
Ordered sequence. Duplicates allowed. Access by index.
Set
No duplicates. Tests membership. Unordered (usually).
Map
Key-value pairs. Unique keys. Fast lookup by key.

The Collections Hierarchy

Collection List Set Map (separate hierarchy) ArrayList LinkedList HashSet TreeSet HashMap TreeMap Italic = interface, regular = concrete class

The Decision Rule

What do you need? Key-value pairs? Yes Map No Duplicates allowed? Yes List No Set

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.

List<String> names = new 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.

Set<String> tags = new HashSet<>();
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.

Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 88);
int s = scores.get("Alice"); // 95

List vs Set: What Changes?

List (ArrayList) [0] "A" [1] "B" [2] "A" ✓ dup ✓ order ✓ index Set (HashSet) "A" "B" "A" ✕ rejected vs

How to Choose: Step by Step

1
Identify the data relationship. Are you pairing keys with values? If yes, use a Map.
2
Check for uniqueness. Must every element be distinct? If yes, use a Set.
3
Consider ordering needs. Do you need index-based access or insertion order? If yes, use a List.
4
Think about performance. For sorted order, prefer TreeSet/TreeMap. For speed, prefer Hash variants.

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.

1
Key-value pairs? No: we just need a list of names.
2
Duplicates? Yes: two students can share a name.
3
Order matters? Yes: enrollment order.
List<String> roster = new ArrayList<>();

Example 2: Unique Visitors

Task: Track which usernames have visited a webpage today. If a user visits twice, they should only be counted once.

1
Key-value pairs? No: just a collection of usernames.
2
Duplicates? No: each visitor counted once.
3
Order matters? Not particularly.
Set<String> visitors = new HashSet<>();

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.

1
Key-value pairs? Yes: name (key) maps to phone number (value).
Map<String, String> phoneBook = new HashMap<>();
phoneBook.put("Alice", "555-1234");
String num = phoneBook.get("Alice"); // "555-1234"

Common Mistakes

✕ Using List for Uniqueness
Storing unique IDs in an ArrayList, then manually checking for duplicates. A HashSet does this automatically.
✕ Using Map Without Keys
Creating a HashMap when there is no natural key-value relationship. If you just need a group of items, use a List or Set.
✕ Assuming Set Has Order
Expecting HashSet to keep insertion order. It does not. Use LinkedHashSet if order matters with uniqueness.

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:

Set<String> s = new HashSet<>();
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?