FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

125 snippets foundClear filters
JavaNumbers & dates

Add days to a date

Parse an ISO date and add calendar days using Java LocalDate.

Java✓ Tested
JavaFiles & paths

Append a line

Append text to a Java file, creating it if necessary.

Java✓ Tested
JavaText validation & formatting

Build text efficiently in a loop

Java: Append to StringBuilder when constructing a string incrementally.

Java✓ Tested
JavaEncoding & structured values

Calculate a SHA-256 content digest

Java: Hash UTF-8 bytes and format the result as lowercase hexadecimal.

Java✓ Tested
JavaGetting Started

Calculate a simple total

Java: Multiply a unit price by a quantity, then add a delivery fee.

Java✓ Tested
JavaPrecise numeric calculations

Calculate beyond the long integer range

Java: Use BigInteger when a fixed-width primitive type would overflow.

Java✓ Tested
JavaStream transformations

Calculate several numeric stream statistics

Java: Use summaryStatistics to calculate count, sum, minimum, maximum, and average in one pass.

Java✓ Tested
JavaGetting Started

Call a library function

Java: Use a standard absolute-value function with a small negative number.

Java✓ Tested
JavaText validation & formatting

Check a filename suffix

Java: Use endsWith for a case-sensitive literal suffix test.

Java✓ Tested
JavaText validation & formatting

Check a string prefix

Java: Use startsWith to check a literal Java prefix without a regular expression.

Java✓ Tested
JavaStream transformations

Check any and all stream elements

Java: Use short-circuiting match operations for predicates over a Java collection.

Java✓ Tested
JavaDates & time zones

Check the Gregorian leap-year rule

Java: Use Year.isLeap rather than checking divisibility by four alone.

Java✓ Tested