Core Libraries

Core Libraries & Data Structures: Overview

Chapter 4 of the Java book: the standard data structures, and the contracts that make objects work inside them. Covers strings in depth and StringBuilder, the Collections Framework (List, Set, Map), the equals/hashCode contract, generics and type erasure, enums, records and sealed types for precise data modeling, and the java.time API for dates, times and time zones. Every example is compiled and run.

Suggest an edit

Core Libraries & Data Structures: Overview

Welcome to Chapter 4. With the object model behind you, these seven lessons introduce the standard library's workhorses — the growable, hashed, and sorted collections you'll reach for every day — along with the contracts that make your own types behave correctly inside them. Two threads run through the chapter. A collection is only as good as the equals/hashCode of what you put in it. And the modern type tools (generics, records, sealed) let you model data so that the compiler catches mistakes for you.

There are seven lessons, in this order:

  1. Strings in Depth — the cost of immutability, StringBuilder, interning, formatting, and text blocks.
  2. The Collections Framework — List and program-to-the-interface, the Iterator, and choosing an implementation.
  3. Sets & Maps — uniqueness and key→value lookup, hash buckets vs ordering, the counting idiom.
  4. equals & hashCode — the contract that makes objects usable as set elements and map keys.
  5. Generics — type parameters, bounds, wildcards (PECS), and type erasure.
  6. Enums & Records — fixed constant sets, immutable data carriers, and a first look at sealed.
  7. Dates & Times — LocalDate and friends, Period vs Duration, time zones, daylight saving, and DateTimeFormatter.

Every code block with a ▶ Run button is live. The most valuable habit in this chapter is to respect the contracts: whenever you put your own type into a HashSet or use it as a HashMap key, ask whether its equals/hashCode are correct — and prefer a record, which gets them right for free.

📘 How to read the Intuition boxes. Each one is built in three moves:

  1. The mechanism — what the compiler and the JVM are actually doing.
  2. A concrete bite — a specific, runnable failure (often a real compiler error), shown so the trap is visible.
  3. The earned rule — the decision heuristic, now justified rather than asserted, plus its cost.
Mark as readBook versionfd26460