JAVA / CATATAN{ }↗Java23 Okt 2025
In Java, the Stream.takeWhile and Stream.dropWhile methods are introduced in Java 9. These operations allow you to process a stream conditionally based on a predicate, controlling how many elements to take or discard from the stream. Here’s how they work: Stream.takeWhile(predicate) Operation: This method takes elements from the stream as long as the given predicate […]
JAVA / CATATAN{ }↗Java22 Okt 2025
Filtering and mapping a stream effectively typically involves three main operations: filtering the elements that meet a specific condition, transforming the elements into another form (mapping), and processing them (e.g., collecting or printing). Here’s an explanation of how to do it effectively, based on the information provided (and generally applicable): 1. Filter The filter method […]
JAVA / CATATAN{ }↗Java22 Okt 2025
Objects.checkIndex, introduced in Java 11, is a utility method for safely validating an index against a given range. It simplifies index validation by throwing well-defined exceptions with meaningful error messages if the index is out of bounds. Syntax public static int checkIndex(int index, int length) index: The index to check. length: The upper bound (exclusive) […]
JAVA / CATATAN{ }↗Java21 Okt 2025
The String.indent(int n) method in Java is a useful tool for formatting multi-line strings by adjusting the amount of leading space (indentation) on each line. Introduced in Java 12, it performs the following for the specified n indentation: Positive n: Adds n spaces to the beginning of each line in the string. Negative n: Removes […]
JAVA / CATATAN{ }↗Java21 Okt 2025
In Java 16, a convenient method Stream.toList() was introduced to simplify collecting elements of a Stream into a List. It provides a more concise alternative to collect(Collectors.toList()), which was used in older versions of Java. Key Differences Stream.toList() produces an immutable list, whereas collect(Collectors.toList()) produces a mutable list. Stream.toList() guarantees immutability, meaning the resulting list […]
JAVA / CATATAN{ }↗Java20 Okt 2025
To use Predicate.not() in streams, you take advantage of its ability to negate an existing predicate. This can be helpful in filter operations where you want to filter out elements that match a given condition instead of including them. Here’s how you can use Predicate.not in streams: Basic Explanation What it does: The Predicate.not() method […]