Lab 01: Generics & Collections

Objective

Master Java generics — bounded type parameters, wildcards (PECS), and the full java.util Collections API: ArrayList, LinkedList, HashMap, LinkedHashMap, TreeMap, and Collections utility methods.

Background

Java generics eliminate ClassCastException at runtime by catching type mismatches at compile time. The PECS rule (Producer Extends, Consumer Super) governs wildcard usage. The Collections API provides ready-made data structures for nearly every use case — choosing the right one is a critical engineering skill.

Time

25 minutes

Prerequisites

  • Java Foundations Labs 01–10

Tools

  • Docker: zchencow/innozverse-java:latest


Lab Instructions

Step 1: Generic Classes & Bounded Types

💡 PECS — Producer Extends, Consumer Super: use List<? extends Number> when you only read (produce values from the list), and List<? super Integer> when you only write (consume values into the list). Mixing both requires an unbound <?> — but then you can only read Object.

📸 Verified Output:


Step 2: TreeMap, PriorityQueue & Deque

📸 Verified Output:


Steps 3–8: Concurrent Collections, Immutable Collections, Comparators, computeIfAbsent, frequency map, Capstone

📸 Verified Output:


Summary

Structure
Ordered
Sorted
Thread-Safe
Use case

ArrayList

Insert order

No

No

Random access list

LinkedList

Insert order

No

No

Queue/Deque/Stack

HashMap

No

No

No

Fast key lookup

LinkedHashMap

Insert order

No

No

Ordered map

TreeMap

Key sorted

Yes

No

Sorted map / range queries

PriorityQueue

Priority

Yes

No

Min/max heap

ConcurrentHashMap

No

No

Yes

Multi-threaded map

Further Reading

Last updated