FIND YOUR NEXT FEW LINES

The snippet library.

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

125 snippets foundClear filters
JavaEncoding & structured values

Encode a form query parameter

Java: URLEncoder uses application/x-www-form-urlencoded rules, including plus signs for spaces.

Java✓ Tested
JavaEncoding & structured values

Encode and decode Base64 bytes

Java: Convert through UTF-8 explicitly before and after Base64 encoding.

Java✓ Tested
JavaGetting Started

Explain a calculation with a comment

Java: Keep the code executable while documenting why a fixed input is used.

Java✓ Tested
JavaText validation & formatting

Extract successive regex matches

Java: Use a Matcher loop to find all unsigned integer tokens in Java text.

Java✓ Tested
JavaText validation & formatting

Extract text between known positions

Java: Java substring uses a start-inclusive, end-exclusive range of UTF-16 indices.

Java✓ Tested
JavaCollections

Filter even numbers

Use a Java stream to collect only even values.

Java✓ Tested
JavaQueues & collection operations

Find set members missing from another set

Java: Use removeAll on a copy to calculate a set difference.

Java✓ Tested
JavaQueues & collection operations

Find shared set members

Java: Copy a set before retainAll when the original must remain unchanged.

Java✓ Tested
JavaStream transformations

Find the first matching stream value

Java: Use findFirst and an explicit default for an empty result.

Java✓ Tested
JavaPrecise numeric calculations

Find the greatest common divisor

Java: BigInteger.gcd calculates an exact nonnegative common divisor.

Java✓ Tested
JavaDates & time zones

Find the last day of a month

Java: Use YearMonth so leap years and month lengths are handled by the calendar library.

Java✓ Tested
JavaDates & time zones

Find the next chosen weekday

Java: TemporalAdjusters.next excludes the starting day even if it already has that weekday.

Java✓ Tested