FIND YOUR NEXT FEW LINES

The snippet library.

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

10 snippets foundClear filters
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
JavaQueues & collection operations

Initialize a map entry only when absent

Java: Use computeIfAbsent when creating a collection for a new key.

Java✓ Tested
JavaQueues & collection operations

Locate a value in a sorted array

Java: Use Arrays.binarySearch only after establishing ascending order.

Java✓ Tested
JavaQueues & collection operations

Preserve map insertion order

Java: Use LinkedHashMap when iteration should follow the order keys were first added.

Java✓ Tested
JavaQueues & collection operations

Remove collection items that satisfy a condition

Java: Use removeIf on a mutable collection to avoid changing it manually inside a for-each loop.

Java✓ Tested
JavaQueues & collection operations

Retrieve the smallest queued value first

Java: PriorityQueue poll removes the smallest value under the natural ordering.

Java✓ Tested
JavaQueues & collection operations

Take an immutable list snapshot

Java: List.copyOf protects the snapshot from later changes to the source list.

Java✓ Tested
JavaQueues & collection operations

Use a deque as a FIFO queue

Java: Add at the tail and remove at the head using ArrayDeque.

Java✓ Tested
JavaQueues & collection operations

Use a deque as a stack

Java: Use push and pop for last-in, first-out behavior.

Java✓ Tested