Set membership and cycle detection
Test, add, and remove members of a `Set`. Use the result of `add` to detect a type already on the active path, and use `finally` to remove its entry when the call exits.
CKTIS / Java
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Test, add, and remove members of a `Set`. Use the result of `add` to detect a type already on the active path, and use `finally` to remove its entry when the call exits.
`Set.size()` returns how many elements the set currently holds, not how many times you tried to add one.
SoftReference<T> in java.lang.ref holds a soft reference to an object. Once the object is no longer strongly reachable, the garbage collector may clear that reference in response to memory demand. Reading it through get() requires handling null.
`firstKey()` and `lastKey()` return the keys at the ends of the map’s sort order. Both throw `NoSuchElementException` if the map is empty.
Learn when an optimistic read can be trusted and how to retry under a shared read lock using the correct stamp to release it.
Use `String.getBytes(Charset)` to encode text as bytes. See why `Euro €` takes 8 bytes in UTF-8 and 12 in UTF-16BE, and why UTF-16BE does not always use two bytes per code point.
Stream.allMatch: practical reference: Stream.allMatch(predicate) is a short-circuiting terminal operation that returns true when every encountered element satisfies the predicate.
Stream.anyMatch: practical reference: Stream.anyMatch(predicate) is a short-circuiting terminal operation that returns true when at least one element satisfies the predicate.
Stream.collect: practical reference: Stream.collect is a terminal mutable reduction that accumulates elements into a container through a Collector or through supplier, accumulator, and combiner functions.
Stream.count: practical reference: Stream.count is a terminal reduction returning as a long the number of elements reaching that point in the pipeline.
Stream.distinct: practical reference: Stream.distinct is a stateful intermediate operation that removes duplicates according to Object.equals.
Stream.dropWhile: practical reference: Stream.dropWhile(predicate) is a stateful intermediate operation that removes the longest matching prefix from an ordered stream.
Stream.filter: practical reference: Stream.filter(predicate) is a stateless intermediate operation that passes only elements for which the predicate returns true.
Stream.findAny: practical reference: findAny is a short-circuiting terminal operation that returns an Optional containing some stream element, or an empty Optional for an empty stream.
Stream.findFirst: practical reference: findFirst is a short-circuiting terminal operation that returns the first element in encounter order for an ordered stream, or Optional.empty when empty.
Stream.flatMap: practical reference: flatMap is a one-to-many intermediate operation that replaces each element with the contents of a mapper-produced stream and flattens those results.
Stream.forEach: practical reference: forEach is a terminal operation that invokes a Consumer for every element but explicitly does not guarantee encounter order in a parallel pipeline.
Use `forEachOrdered` when a terminal action must follow the stream’s encounter order, even in a parallel pipeline. Ordinary `forEach` does not make that guarantee in parallel mode.
Stream.limit: practical reference: limit is a short-circuiting stateful intermediate operation that yields at most maxSize elements, taking the first elements for an ordered stream.
Stream.map: practical reference: map is a stateless one-to-one intermediate operation that lazily applies a Function to each consumed element and produces a stream of results.
Stream.mapMulti: practical reference: mapMulti is a one-to-zero-or-many intermediate operation whose mapper sends each result to a supplied Consumer instead of creating a separate stream.
Map filtered objects to a primitive stream and sum its values. Choose `int` or `long` before summing based on the range the total needs.
Stream.max: practical reference: max is a terminal reduction that returns the greatest stream element under the supplied Comparator, or Optional.empty for an empty stream.
Stream.min: practical reference: min is a terminal reduction that returns the least stream element under the supplied Comparator, or Optional.empty for an empty stream.