JAVA / NOTES{ }↗Java22 Oct 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 / NOTES{ }↗Java21 Oct 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 / NOTES{ }↗Java21 Oct 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 / NOTES{ }↗Java20 Oct 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 […]
JAVA / NOTES{ }↗Java20 Oct 2025
In Java, Collectors::teeing is a feature introduced in Java 12 that allows you to collect elements of a stream using two different collectors and then combine the results using a BiFunction. Syntax: static <T, R1, R2, R> Collector<T, ?, R> teeing(Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, […]
JAVA / NOTES{ }↗Java19 Oct 2025
The List.of, Set.of, and Map.of factory methods were introduced in Java 9 to create immutable collections in a simpler and more concise way. These methods directly create unmodifiable instances of List, Set, or Map. Usage of List.of The List.of method is used to create an unmodifiable List containing the provided elements. The key characteristics are: […]