Advanced

Collections Framework

Use List, Set, and Map for structured data storage.

What you will see

90 false

About 2 min of reading

Try It

Store data with the right structure. Use a List for ordered access, a Set for uniqueness, and a Map for labelled lookup, then read back the value each one returns.

1

Build an ordered, resizable ArrayList backed by the List interface. Add two names and print each in order with a for-each loop.

import java.util.ArrayList; import java.util.List; class Main { public static void main(String[] args) { List names = new ArrayList<>(); names.add("Ada"); names.add("Bob"); for (String n : names) { System.out.println(n); } } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Ada Bob
List it is ordered and keeps duplicates, so use the List interface as the type, you can swap ArrayList for a LinkedList later without changing callers.
2

A Set holds only unique elements. Add the same value twice and see the Set ignore the duplicate, size reports only what was actually stored.

import java.util.HashSet; import java.util.Set; class Main { public static void main(String[] args) { Set ids = new HashSet<>(); ids.add(1); ids.add(1); System.out.println(ids.size()); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
1
HashSet ignores duplicates and gives no ordering. Use it when membership is all you care about, not for sequences.
3

A Map pairs unique keys with values. Store a score under a key, read it back with get, then ask whether another key exists. get returns null for a missing key.

import java.util.HashMap; import java.util.Map; class Main { public static void main(String[] args) { Map scores = new HashMap<>(); scores.put("Ada", 90); int grade = scores.get("Ada"); boolean has = scores.containsKey("Cy"); System.out.println(grade); System.out.println(has); } }

Before you run it, say out loud what it will print. Then open the answer.

What it prints
90 false
Keys must be unique and hashable. get returns null when the key is absent, so guard against null before treating the value as a number.

Data Flow

Collections store elements; the right structure gives indexed, set, or map access depending on the need.
1 Choose collection by access pattern
↓
2 Add elements
↓
3 Access by index, membership, or key
↓
4 Iterate with for-each or streams
Why this works
Generics and type safety

Generics enforce types at compile time:

List<String> list = new ArrayList<>();
// list.add(5);  // compile error: not a String

The <String> type parameter ensures only strings can be stored. Without generics, you'd need casts and risk ClassCastException at runtime.

When you have a choice

List vs Set vs Map

CollectionOrderDuplicatesAccess
ArrayListYesYesby index
HashSetNoNoby membership
HashMapKey orderUnique keysby key

Use List for ordered sequences, Set for uniqueness, Map for labelled lookup. Choose LinkedHashMap/TreeMap when you need ordering.

Examples

Using ArrayList

Ordered collection

List<String> names = new ArrayList<>();
names.add("Ada");
names.add("Bob");
for (String n : names) {
    System.out.println(n);
}

ArrayList is an ordered, resizable collection. Use the List interface so you can swap implementations.

Using HashSet

Unique values

Set<Integer> ids = new HashSet<>();
ids.add(1);
ids.add(1);   // ignored, already present
System.out.println(ids.size());   // 1

Set holds unique elements. HashSet gives O(1) average membership tests but no ordering.

Using HashMap

Key/value storage

Map<String, Integer> scores = new HashMap<>();
scores.put("Ada", 90);
int grade = scores.get("Ada");   // 90
boolean has = scores.containsKey("Cy");

Map maps keys to values. Keys are unique and hashable. get returns null for a missing key unless you use getOrDefault.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Object-Oriented Programming
Previous Lesson
Object-Oriented Programming
Next Lesson
Streams and Lambdas
Streams and Lambdas →