Results for “Java”
Small, practical examples. Search by task or narrow things down below.
Replace one collection element
JavaScript: Update an existing element at a known valid index.
JavaScript✓ Tested
Represent a finite set of states
Java: Use an enum to prevent arbitrary strings from representing application states.
Java✓ Tested
Require two conditions to be true
Java: Combine a lower and an upper bound to check whether a value is in range.
Java✓ Tested
Require two conditions to be true
JavaScript: Combine a lower and an upper bound to check whether a value is in range.
JavaScript✓ Tested
Resolve a relative URL against a base
JavaScript: A trailing slash makes the final base path component a directory.
JavaScript✓ Tested
Retrieve the smallest queued value first
Java: PriorityQueue poll removes the smallest value under the natural ordering.
Java✓ Tested
Return a value from a switch expression
Java: Use an exhaustive switch expression with a fallback for other input values.
Java✓ Tested
Reverse a copy while preserving the original
JavaScript: Copy before reverse because reverse mutates the array it receives.
JavaScript✓ Tested
Round a decimal with an explicit rule
Java: Construct BigDecimal from text to avoid importing a binary floating-point approximation.
Java✓ Tested
Run a task on an executor and close it
Java: Java 21 ExecutorService supports try-with-resources to shut down after submitted work.
Java✓ Tested
Run cleanup after a handled exception
Java: A finally block executes whether the protected operation succeeds or fails.
Java✓ Tested
Run cleanup after a promise settles
JavaScript: finally runs after success or failure and normally preserves the original outcome.
JavaScript✓ Tested