Skip to content

Chapter 18 of 37

Collections: List, Set, and Map

Choose ordered, unique, keyed, sorted, and concurrent collection implementations.

44 minutes 10 quick checksBy Subha Prasad
Lesson 18 of 37Course navigation

Lesson content

Read, practise, then check your understanding

List is ordered and permits duplicates, Set enforces uniqueness, and Map associates unique keys with values. Program to an interface and choose an implementation from access pattern and guarantees.

Common choices

List<String> names = new ArrayList<>();
Set<String> unique = new HashSet<>();
Map<String, Integer> counts = new HashMap<>();

names.add("Java");
unique.addAll(names);
counts.merge("Java", 1, Integer::sum);

ArrayList is the default sequence; LinkedList is rarely faster in real workloads. HashSet/HashMap offer average constant-time operations and depend on correct equals/hashCode. TreeSet/TreeMap maintain sorted order in logarithmic time; LinkedHash variants preserve encounter order. List.of, Set.of, and Map.of create unmodifiable collections that reject null. Concurrent access needs immutability, synchronization, or concurrent implementations—not merely a synchronized individual call.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes List?
Which Java term matches this description: An ordered collection that permits duplicates.
Which statement best describes Set?
Which Java term matches this description: A collection that contains no duplicate elements by its equality contract.
Which statement best describes Map?
Which Java term matches this description: A key-to-value association that is not itself a Collection.
Which statement best describes ArrayList?
Which Java term matches this description: A resizable-array List with efficient indexed access.
Which statement best describes HashMap?
Which Java term matches this description: A hash-based Map with average constant-time basic operations.

0 of 10 checks passed

Your progress is saved on this device.