Lesson Overview
Use ArrayList, HashMap, generics and enhanced iteration to manage changing data safely.
Portfolio focus: Replace an array with an ArrayList.
Starter: think before typing
Before running this data structures example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `Programming 1 = 20 credits Programming 2 = 0 credits`; predict how the focus line helps produce that evidence.
Learning Objectives
- Choose arrays or collections appropriately.
- Use generic types to reduce casting errors.
- Iterate over collections safely.
- Use maps for key-value lookup.
Learning Outcomes
- By the end of the lesson, you can choose arrays or collections appropriately.
- By the end of the lesson, you can use generic types to reduce casting errors.
- By the end of the lesson, you can iterate over collections safely.
- By the end of the lesson, you can use maps for key-value lookup.
Why this idea exists
Collections are reusable answers to common data-structure problems: storing ordered items, looking up values by key, avoiding duplicates and traversing groups of objects.
Generics were added to Java to make collections safer and clearer. Before generics, programmers often had to cast values out of collections, which moved type mistakes from early checking into later failures.
Choosing a collection is a design decision. An `ArrayList` suggests ordered traversal, a `HashMap` suggests keyed lookup, and each choice communicates assumptions about how the data will be used.
Deep dive
Mechanism in this example
The important mechanism is visible around `credits.put("Programming 1", 20);`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.
Design pressure
Choosing a collection is a design decision. An `ArrayList` suggests ordered traversal, a `HashMap` suggests keyed lookup, and each choice communicates assumptions about how the data will be used.
Failure mode to watch
For Collections, Generics and Iteration, deliberately disturb the assumption behind `credits.put("Programming 1", 20);`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the data structures design.
Extension step
Extend the example by doing this: Use a HashMap for module credits. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.
Portfolio standard
The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: explain what generics prevent.
Lesson visual

Type this and run it
Create CollectionsGenericsIterationDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CollectionsGenericsIterationDemo {
public static void main(String[] args) {
List<String> modules = new ArrayList<>();
modules.add("Programming 1");
modules.add("Programming 2");
Map<String, Integer> credits = new HashMap<>();
credits.put("Programming 1", 20);
for (String module : modules) {
System.out.println(module + " = " + credits.getOrDefault(module, 0) + " credits");
}
}
}Build and run it with:
javac CollectionsGenericsIterationDemo.java && java CollectionsGenericsIterationDemoExpected baseline: Programming 1 = 20 credits
Programming 2 = 0 credits
Run the code in your browser
Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates data structures and compare the new behaviour with the reference output.
Programming 1 = 20 credits
Programming 2 = 0 creditsLine-by-line explanation
Read the code as a sequence of responsibilities. The focus line for this lesson is credits.put("Programming 1", 20);; the surrounding lines prepare it, use its result or make the behaviour observable.
import java.util.ArrayList;In Collections, Generics and Iteration, this imports ArrayList: a resizable list implementation for changing collections.
import java.util.HashMap;In Collections, Generics and Iteration, this imports HashMap: a map implementation for key-based lookup.
import java.util.List;In Collections, Generics and Iteration, this imports List: the ordered collection type used to hold several values.
import java.util.Map;In Collections, Generics and Iteration, this imports Map: the key-value collection interface.
public class CollectionsGenericsIterationDemo {This names the runnable class for the Collections, Generics and Iteration example, giving the compiler and JVM one clear unit to build.
public static void main(String[] args) {This is the program entry point. In Collections, Generics and Iteration, it keeps the demonstration of data structures in one traceable starting script.
List<String> modules = new ArrayList<>();This creates a collection object in Collections, Generics and Iteration, choosing the data structure before modules is stored or looked up.
modules.add("Programming 1");This calls modules.add with "Programming 1" in Collections, Generics and Iteration. Look for the method definition to see what work actually happens.
modules.add("Programming 2");This calls modules.add with "Programming 2" in Collections, Generics and Iteration. Look for the method definition to see what work actually happens.
Map<String, Integer> credits = new HashMap<>();This creates a collection object in Collections, Generics and Iteration, choosing the data structure before credits is stored or looked up.
credits.put("Programming 1", 20);This calls credits.put with "Programming 1", 20 in Collections, Generics and Iteration. Look for the method definition to see what work actually happens.
for (String module : modules) {This starts repetition in Collections, Generics and Iteration. Check the initial value, the stopping condition and the update on each pass.
System.out.println(module + " = " + credits.getOrDefault(module, 0) + " credits");This prints module + " = " + credits.getOrDefault(module, 0) + " credits" as the observable evidence for Collections, Generics and Iteration. The output lets the learner check whether the data structures idea behaved as predicted.
}This closes the innermost Collections, Generics and Iteration block, so the immediately preceding method, branch or loop has finished.
}This closes the outer Collections, Generics and Iteration structure, returning the reader to the surrounding class or file.
}This closing brace number 3 completes another layer of the Collections, Generics and Iteration source structure Java has been checking.
Worked example
From code to explanation
Problem: Use Collections, Generics and Iteration to complete a small portfolio-quality step: Replace an array with an ArrayList.
Method: Locate the line `credits.put("Programming 1", 20);`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.
Reveal worked answer
The checked run should produce `Programming 1 = 20 credits Programming 2 = 0 credits`. A strong answer links the result back to data structures: what was created, selected, stored, called or protected, and why that matters for the portfolio task.
Trace the program
Before: Before the key operation, identify the relevant value, object, branch or resource that the data structures concept depends on.
During: Trace `credits.put("Programming 1", 20);` as the Collections, Generics and Iteration example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.
After: Compare the run with the expected evidence: `Programming 1 = 20 credits Programming 2 = 0 credits`.
Change: Now use a hashmap for module credits, run again, and explain the smallest reason the behaviour changed.
Common misconception
A common mistake in collections, generics and iteration is treating the example as a finished answer. For data structures, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?
Quick checks
1. In this Collections, Generics and Iteration example, what is the best reason to focus on `credits.put("Programming 1", 20);`?
2. Which evidence is strongest after you edit and rerun this example?
How to study this lesson
For Collections, Generics and Iteration, predict how data structures changes the run before you press Run.
Use the first portfolio task as your main edit: Replace an array with an ArrayList.
Use the second task as your variation: Use a HashMap for module credits.
Finish with evidence, not a diary entry: Explain what generics prevent.
Portfolio Practice
- Replace an array with an ArrayList.
- Use a HashMap for module credits.
- Explain what generics prevent.
Final self-check
Can you explain the key operation?
Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output Programming 1 = 20 credits
Programming 2 = 0 credits appears.
Can you justify the portfolio evidence?
Your evidence should include the original run, one edited run, and a short note explaining how the edit affected data structures.
Study route
Practise data structures by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.
Next, move into Time, Space and Speed Trade-offs and carry forward one improvement from this lesson into the next program.
