#java Clear
Tags #php #kotlin #bash #go #sql #rust #typescript #html #java #python #files #utils #strings #http #concurrency #async #json #arrays #security #types #crypto #database #dates #format
Java Virtual Threads (Java 21+)
Java 21's virtual threads are millions-of-them cheap — JVM multiplexes them onto a small carrier pool. Replaces async/reactive code for most I/O-bound workloads: just write blocking code that doesn't actually block a kernel thread.
Java Collectors.toMap with Merge Function
`toMap` builds a `Map<K, V>` from a stream. The 3-arg form takes a merge function for handling duplicate keys — without it, duplicates throw IllegalStateException.
Java java.time — Format and Parse
`DateTimeFormatter` replaces `SimpleDateFormat` (which was not thread-safe). Constants for the common formats; pattern strings for custom layouts.
Java Collectors.groupingBy — Group by Key
Like SQL GROUP BY for streams. Produces a `Map<K, List<V>>` (or any downstream collector you specify). Indispensable for analytics-style aggregations.
Java Optional Chains — flatMap, filter
`flatMap` lets you chain Optional-returning methods without nested `if`. `filter` short-circuits to empty if the predicate fails. The functional equivalent of safe-navigation `?.`.
Java HttpClient — Async (CompletableFuture)
`sendAsync` returns a `CompletableFuture<HttpResponse<T>>` — chain it like any other CF. Fan out N requests, wait for all with `CompletableFuture.allOf`.
Java Optional — Stop Returning Null
`Optional<T>` makes "may be absent" part of the type signature so callers can't forget the null check. `map`, `orElse`, `ifPresent` chain naturally — no `if (x != null)` boilerplate.
Java JUnit 5 — Parameterized Tests
`@ParameterizedTest` runs the same test with multiple inputs. Use `@CsvSource`, `@MethodSource`, or `@EnumSource` for data. Eliminates copy-paste in test classes.
Java Optional with Streams
`Optional::stream` (Java 9+) bridges optionals into the Stream API — collect all the `Some` values from a list of optionals, drop the empties, in one pipeline.
Java JUnit 5 — Basic Test
JUnit 5 is the modern standard. `@Test` marks a method as a test; `Assertions.*` provides the assertion API. `@BeforeEach` runs setup before every test.
Java Collectors.partitioningBy — Split by Predicate
Special case of groupingBy when the key is boolean — returns a `Map<Boolean, List<T>>` for the "true" and "false" buckets. Slightly more efficient than groupingBy.
Java Builder Pattern
For objects with many optional parameters, a Builder beats a constructor with 12 args (or a half-initialized object you have to call setters on). Method chaining + a `build()` step that validates.
Java Path Operations — Resolve, Relativize, Normalize
`Path` (NIO) replaces `File` for new code. Operator-like methods compose paths cleanly across OSes — and the underlying file isn't touched until you do I/O.
Java Atomic File Write (move with ATOMIC_MOVE)
Write to a temp file in the same directory, then `Files.move` with `ATOMIC_MOVE` to the target. Readers never see a half-written file — critical for config / state files.
Java HttpClient — Synchronous GET (Java 11+)
`java.net.http.HttpClient` ships in the JDK — no Apache HttpClient or OkHttp needed for most cases. Reuse one client across the app; configure timeouts and follow-redirects up front.
Java Stream.generate / iterate — Infinite Streams
Build streams from a seed + a function. `generate(supplier)` calls the supplier each time; `iterate(seed, next)` applies a unary op. Always pair with `limit` to bound them.
Java CountDownLatch — Wait for N Events
Block one or more threads until a count of events occurs. Set the count up front; each event calls `countDown()`; waiters call `await()`. Classic "wait until all workers are ready" pattern.
Java Sorting with Comparator
`Comparator.comparing`, `thenComparing`, and `reversed()` chain into expressive multi-field sorts. Way cleaner than the old Comparable spaghetti.