Java 8 example to sort stream of objects by multiple fields using comparators and Comparator.thenComparing() method. This method returns a lexicographic-order comparator with another comparator. It gives the same effect as SQL group by clause.
Learn to sort stream of numbers and strings in ascending (natural order) and descending orders (reverse order) in Java 8.
Learn to create generic functional interfaces with and without type restrictions in Java 8 and later. Note that functional interfaces permit exactly one abstract method.
Learn to convert stream to list using Collectors.toList() and Collectors.toCollection() APIs. This post contains multiple examples for collecting stream elements to list under different usecases.
Learn to convert stream to array using Stream.toArray() API. This post contains multiple examples for collecting stream elements to array under different usecases.
Learn to collect stream elements into Map using Collectors.toMap() and Collectors.groupingBy() methods using Java 8 Stream APIs. Convert stream to map using Java stream APIs. 1. Stream Elements with unique map keys – Collectors.toMap() If the stream elements have the unique map key field then we can use Collectors.toMap() to collect elements to map in […]
Is it possible to reuse streams in Java? Learn the alternative of Java stream reuse. 1. Can we reuse stream? No. Java streams, once consumed, can not be reused by default. As Java docs say clearly, “A stream should be operated on (invoking an intermediate or terminal stream operation) only once. This rules out, for […]
Java Stream interface has two methods i.e. findFirst() and findAny(). Both method looks very much similar but they may behave differently in certain conditions. In this post, learn the difference between findFirst() and findAny() methods. 1. Stream findFirst() method 1.1. Description This method returns an Optional describing the first element of this stream. In case […]
Java Stream noneMatch (Predicate predicate) is short-circuiting terminal operation which is used to check if no element of the stream match the provided predicate.
Java Stream allMatch (Predicate predicate) is short-circuiting terminal operation which is used to check if all the elements of the stream match the provided predicate. 1. Stream allMatch() method 1.1. Syntax Here predicate a non-interfering, stateless predicate to apply to elements of the stream. 1.2. Description It is a short-circuiting terminal operation. It returns whether […]
Java Stream anyMatch (Predicate predicate) is terminal-short-circuiting operation which is used to check if the stream contains any matching element with provided predicate.
Java examples to create infinite stream of data/elements. We will use Stream.generate() and Stream.iterate() methods to get the infinite streams.
We can use Stream.skip(long n) method to skip first ‘n’ elements from a stream. skip() method returns a stream consisting of the remaining elements of this stream, after the specified n elements have been skipped.
We can use Stream.limit(long maxSize) method to retrieve elements while they must not be greater than a certain maximum count. limit() method returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length. limit(N) method returns first N elements in the encounter order. 1. Stream.limit() – Syntax […]
Java 8 Stream interface has peek(Consumer action) method which returns a new stream consists of all the elements of original stream after applying the method argument Consumer action.
The difference between map() vs flatMap() with example. map() is used for transformation only, but flatMap() is used for both transformation and flattening.
Learn to use Java Stream flatMap() method which is used to flatten a stream of collections to a stream of elements combined from all collections.
Learn to use Java Stream map() method which produces one output value of a different type ‘X’ for each input value of type ‘Y’.
Learn to use Stream min() method to select the smallest element in the stream according to the comparator provided in its argument.
Learn to use Stream max() method to select the largest element in the stream according to the comparator provided in its argument. 1. Stream.max() method This is a terminal operation. So stream cannot be used after this method is executed. Returns the maximum/largest element of this stream according to the provided Comparator. This is a […]