The snippet library.
Small, practical examples. Search by task or narrow things down below.
Encode a form query parameter
Java: URLEncoder uses application/x-www-form-urlencoded rules, including plus signs for spaces.
Java✓ Tested
Encode and decode Base64 bytes
Java: Convert through UTF-8 explicitly before and after Base64 encoding.
Java✓ Tested
Explain a calculation with a comment
Java: Keep the code executable while documenting why a fixed input is used.
Java✓ Tested
Extract successive regex matches
Java: Use a Matcher loop to find all unsigned integer tokens in Java text.
Java✓ Tested
Extract text between known positions
Java: Java substring uses a start-inclusive, end-exclusive range of UTF-16 indices.
Java✓ Tested
Filter even numbers
Use a Java stream to collect only even values.
Java✓ Tested
Find set members missing from another set
Java: Use removeAll on a copy to calculate a set difference.
Java✓ Tested
Find shared set members
Java: Copy a set before retainAll when the original must remain unchanged.
Java✓ Tested
Find the first matching stream value
Java: Use findFirst and an explicit default for an empty result.
Java✓ Tested
Find the greatest common divisor
Java: BigInteger.gcd calculates an exact nonnegative common divisor.
Java✓ Tested
Find the last day of a month
Java: Use YearMonth so leap years and month lengths are handled by the calendar library.
Java✓ Tested
Find the next chosen weekday
Java: TemporalAdjusters.next excludes the starting day even if it already has that weekday.
Java✓ Tested