The snippet library.
Small, practical examples. Search by task or narrow things down below.
Load a small properties configuration
Java: Use a Reader to make the configuration character encoding explicit.
Java✓ Tested
Locate a value in a sorted array
Java: Use Arrays.binarySearch only after establishing ascending order.
Java✓ Tested
Measure a duration between instants
Java: Use Duration for elapsed timeline time and retrieve total seconds.
Java✓ Tested
Measure a short text value
Java: Read the length of an ASCII string. Non-ASCII text may use multiple code units or bytes depending on the language.
Java✓ Tested
Measure UTF-8 bytes separately from characters
Java: Encode a Java string before measuring its serialized byte length.
Java✓ Tested
Move a file to a new name
Java: Files.move changes the path and returns the destination Path.
Java✓ Tested
Name a value that stays fixed
Java: Use a constant declaration or the language’s constant naming convention for a fixed limit.
Java✓ Tested
Narrow an object with a pattern variable
Java: Pattern matching avoids a separate cast after a successful instanceof check.
Java✓ Tested
Normalize relative path components
Java: Path.normalize removes redundant dot components without accessing the filesystem.
Java✓ Tested
Parse a date with an explicit input pattern
Java: Use DateTimeFormatter for a non-ISO date representation.
Java✓ Tested
Parse a UUID and inspect its version
Java: Use UUID.fromString for a standard UUID value.
Java✓ Tested
Parse an integer in another base
Java: Supply an explicit radix to Integer.parseInt.
Java✓ Tested