Java Foundations·Objects that hold up

Collections and generics

The Java collections framework gives you the right container and type safety.

Beginner 14 min read Lesson 7 of 300 Challenge included
What you will be able to do
  • Use ArrayList for indexed sequences and HashSet for membership.
  • Use HashMap for key lookup and LinkedHashMap when insertion order matters.
  • Program to interfaces: List, Set and Map.

In plain English

List preserves order, Set enforces uniqueness and Map stores key-value pairs. Generics let the compiler enforce element types. Choose an implementation by access pattern, not by habit.

Collections.javaJava
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

List<String> lessons = new ArrayList<>();
lessons.add("HTML");
lessons.add("CSS");

Map<String, Integer> minutes = Map.of(
    "HTML", 8,
    "CSS", 12
);

for (String lesson : lessons) {
    System.out.println(lesson + ": " + minutes.get(lesson));
}
Your turn

Count unique topics

Easy

Create a Set from a list containing duplicate topic names and print its size.

JAVA · starter
List<String> topics = List.of("HTML", "HTML", "CSS");
Check your understanding before moving on.
Knowledge checkQuestion 1

Which collection rejects duplicate values?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 7 of 300 · 0% of course