TreeMap(): natural key ordering and grouping
`TreeMap()` creates an empty map that sorts keys by their natural order. Use `TreeMap::new` as the map supplier when `groupingBy` should return sorted group keys.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
`TreeMap()` creates an empty map that sorts keys by their natural order. Use `TreeMap::new` as the map supplier when `groupingBy` should return sorted group keys.
In Java SE 21, get a handle to a plain `int` field, choose memory ordering for reads and writes, and conditionally update the value in one atomic step.
Learn how five WeakHashMap operations behave in Java SE 21 when keys are weak, values are strong, and null can mean more than one thing.
WeakReference<T> holds a weak reference that does not prevent the garbage collector from reclaiming an object. Reading it through get() may return null; clear() clears the reference without destroying the object itself.
Create a zoned date-time, convert it without changing the instant, format it for display, and edit local fields while accounting for time-zone transitions.
In Java 21, create a zoned date-time, explicitly select either occurrence of a repeated local time, and change zones without confusing the instant with the clock reading.
Use `ZoneId.of` to select a regional time zone or fixed UTC offset and display the same instant in Warsaw, New York, and Tokyo.
Look up the UTC offsets in Warsaw and New York at the same instant in 2025, and see why a fixed offset cannot replace a regional time zone.
ZoneOffsetTransition describes a change in UTC offset. The isGap() method identifies a gap, while getDateTimeAfter() returns the local date-time at the transition boundary using the new offset.
ZoneRules describes changes to a zone's offset. Its getValidOffsets and getTransition methods detect nonexistent or ambiguous local times before an offset is selected.
Java program architecture: Java architecture uses package and JPMS module boundaries: module-info.java declares requires, exports, opens, uses, and provides, while the JVM loads compiled bytecode. A fluent builder with a recursive self type, such as B extends Builder<B>, preserves the concrete builder type across chained methods and inheritance.
Asynchrony and concurrency in Java: Java 21 concurrency includes Thread, ExecutorService, Future, CompletableFuture, and virtual threads, while the Java Memory Model defines visibility through happens-before relations. The Java Memory Model (JMM) defines cross-thread reads; for ordinary mutable or non-final state, safe publication requires happens-before from completed initialization to the reader.
Boxing converts a primitive value to a wrapper object, while unboxing retrieves its value. Learn how == differs from equals and why null and shared wrapper instances can cause surprises.
Java collections: The Java Collections Framework separates Collection, List, Set, Queue, and Map interfaces from implementations, with equality, ordering, and mutability contracts determined by the type.
Comments and documentation in Java: Java recognizes line //, block /* ... */, and documentation /** ... */ comments; javadoc interprets tags and generates API documentation from doc comments.
Conditional control flow in Java: Java selects control flow with boolean if/else or a switch statement or expression, which in Java 21 supports type and record patterns, when guards, and case null.
Data pipelines in Java: A Java data pipeline commonly combines a source, lazy Stream intermediate operations, and one terminal operation; elements are pulled only as needed.
A `final` field does not freeze an array. Copying mutable data on both input and output keeps shared references from changing an object's state.
Copy a list in the constructor and in `getRoles()` so callers cannot modify the list stored in a DTO. A `final` field alone does not provide that protection.
Separate data sent across a boundary from domain state and behavior. Map five fields explicitly to create independent snapshots that never carry `passwordHash`.
Exceptions and error handling in Java: Java reports failures as Throwable: Error normally represents JVM-level problems, Exception covers application conditions, and RuntimeException is unchecked.
Expressions and operators in Java: Java expressions evaluate left to right under fixed precedence, numeric promotion, and arithmetic, logical, bitwise, comparison, and assignment operators.
Filtering and transformations in Java: Filtering keeps elements satisfying a Predicate, map transforms each element, and flatMap expands each result into zero or more values in Stream or Optional.
Finite-state machines in Java: A finite-state machine models state and event as enums or sealed types, with a transition function selecting the next state, often through an exhaustive switch expression.