An LRU cache with LinkedHashMap
Build a two-entry cache in Java SE 21 and see why reading a key changes which entry will be evicted on the next insertion.
CKTIS / Java
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Build a two-entry cache in Java SE 21 and see why reading a key changes which entry will be evicted on the next insertion.
Check how many elements a list contains before reading by index. When clocks go back and a local time occurs twice, read both valid UTC offsets explicitly.
`List.add(E)` appends one element to a list and returns `true` on success. The indexed overload inserts an element at a position you choose.
Use `List.addAll` to append the elements of a collection or insert them at a chosen index. The `boolean` result tells you whether the destination list changed.
`List.clear()` removes every element from an existing list. It returns no value, and not every list supports the operation.
`List.copyOf` returns an unmodifiable snapshot in the source collection’s iteration order. It shares element objects and rejects `null`.
Read an element by its zero-based index, use size() to check the bounds, and distinguish a stored null from an out-of-range index.
Find the first occurrence of an element in a Java list. The result is a zero-based index, or `-1` when no element matches.
`List.isEmpty()` returns `true` when a list has no elements. Use the result to choose a branch or to guard a read of the first or last element.
`List.of` creates an unmodifiable list from the supplied elements. The no-argument `List.of()` returns an empty list. None of the overloads accepts `null` elements.
Choose the right `List.remove` overload: one returns the element removed at an index, while the other reports whether it removed the first matching value.
`List.set(int,E)` replaces the element at an existing index and returns the old element. It neither changes the list's size nor shifts any other elements.
`List.size()` returns the number of elements in a list as an `int`. Use it to check whether there are enough elements or to set an exclusive upper bound for indexing.
List.sort reorders an existing list using a comparator or natural ordering while preserving the relative order of elements that compare as equal.
`List.subList` returns a view backed by the original list, not a copy. The start index is included and the end index is excluded.
Use the sign of `LocalDate.compareTo` to order dates. In a date-only ordering, orders on the same day compare as equal even when they have different titles.
Use `LocalDate.equals` to check whether two `LocalDate` values represent the same ISO date. Use `isEqual` to compare the underlying day across calendars.
Build an ISO date from three fields, choose the overload that fits your input, and compare the resulting dates by value rather than by reference.
Create an ISO calendar date, shift it by calendar days without changing the original, check whether one date is later than another, and turn a date into text.
Build dates from fields, parse ISO or custom text, and format dates and date-times without assigning a time zone.
Build local dates and times from fixed inputs, find the Monday of the same ISO week, and choose the overload that returns the type you need.
Create a local date-time with `LocalDateTime.of`, then add hours or minutes without changing the original value.
Track a running `long` maximum with `LongAccumulator`, choose an identity that works for your input range, and understand what `get()` can report during concurrent updates.
Use `LongAdder` to collect statistics updated by multiple threads. Learn which methods change the total, when a read is exact, and why `sumThenReset()` does not create an atomic boundary between reporting intervals.