Java Stream contains, containsAny and containsAll Examples
Learn to create a Java Stream utility class with contains(), containsAll() or containsAny() methods and learn to use them with examples.
Learn to create a Java Stream utility class with contains(), containsAll() or containsAny() methods and learn to use them with examples.
Stream mapMulti() is a specialized form of the map() method that transforms each element of a stream into one or multiple output elements or none at all.
JPAstreamer helps in fetching and processing the JPA or hibernate entities in the same way as we process the POJOs using Java 8 Stream API.
Learn to handle the checked exceptions thrown from the methods used in Stream operations in Java 8 using safe method extraction and Optional.
Learn to use Collectors.groupingBy() method to group and aggregate the Stream elements similar to ‘GROUP BY‘ clause in the SQL. Stream -> groupingBy() -> Map of elements after applying ‘group by’ operation 1. Collectors.groupingBy() Method 1.1. Syntax The groupingBy() method returns a Collector implementing a “GROUP BY” operation on Stream …
Learned the root cause and solution of the Java Stream exception “IllegalStateException: stream has already been operated upon or closed”.
Java Streams have gained a lot of awareness and being able to iterate through with indices can be helpful. Learn a few ways with examples.
Learn to remove and update elements of a Collection using Stream.filter() and Strea,map() methods in Java with examples.
Learn to filter a Java HashMap by List of keys and collect the matching entries into a submap or matching values in a List.
Learn to convert a Stream into immutable/unmodifiable collection using Stream methods such as stream() and Collectors.toUnmodifiableList().
Debugging Java streams can be challenging. In this post, learn to debug streams as their elements are processed in the chained method calls.
Learn to create and operate on the streams of primitive types (IntStream, LongStream and DoubleStream) in Java with examples.
Learn to add items to a Java Stream. Remember that a Stream is not a data structure or collection that can store values. To add items to an existing Stream, we need to : Create a new Stream with items to be added Concatenate with the first stream to get …
Learn to get an object with the latest date (max date) from a Stream of custom objects. We will use a custom Comparator for comparing the Date values stored in the custom objects. This example uses Employee class. We will create a program to get the youngest employee in a …
Java 8 Stream.findAny() returns an Optional describing any element of Stream if Stream is non-empty. It returns an empty Optional if the stream is empty.
Java Stream findFirst() returns an Optional describing the first element of stream if Stream is non-empty, or an empty Optional if the stream is empty.
Learn to find the last element of a stream in Java 8 or later. We will learn to use finite as well as infinite streams as well.
Few simple examples to find or count the duplicates in stream and remove the duplicates from stream in Java 8. We will use ArrayList to provide stream of elements including duplicates.
Learn to convert Iterable or Iterator to Stream. It may be desired at times when we want to utilize excellent support of lambda expressions and collectors in Java 8 Stream API.
Java IntStream represents a stream of primitive int-valued elements supporting sequential and parallel aggregate operations.
Learn about Collectors.teeing() method (added in Java 12), method syntax and how to apply teeing() method in various usecases in Java.
Learn to filter a stream of items for multiple if-else style conditions using complex predicates and process filtered items as required.
Learn to join a stream of strings with a delimiter, prefix, and suffix using Collectors.joining(), String.join(), and append elements using StringBuilder.
The Java 8 Stream.concat() method merges two streams into one stream. The combined stream consists of all the elements of both streams.
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) using Stream.sorted() method.
Learn to create generic functional interfaces with and without type restrictions in Java. Learn to create specialized functional interfaces.
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 items into Map using Collectors.toMap() and Collectors.groupingBy() when Map keys are distinct or duplicates.
Java streams cannot be reused. Once a terminal operation is performed on a stream, it is considered consumed and cannot be reused.
Java Stream has two methods findFirst() and findAny() for retrieving elements. Learn the difference between both methods in parallel streams with examples.
Java Stream noneMatch(predicate) is a short-circuiting terminal operation to check if no element in the stream matches the given predicate.
Java Stream allMatch() is a short-circuiting terminal operation that is used to check if all the elements in the stream satisfy the provided predicate. 1. Stream allMatch() Method 1.1. Syntax Here predicate a non-interfering, stateless predicate to apply to all the elements of the stream. The allMatch() method returns always …
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 8 examples to create an infinite stream of elements. We will use Stream’s generate() and iterate() methods to get the infinite streams.
Java Stream skip(n) method returns a new stream consisting of the remaining elements of this stream, after the n elements have been skipped.
We can use Stream.limit(long) to retrieve elements while they must not be greater than a certain maximum count. Java 8 Stream limit() example
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 Java Stream max() method to select the largest element in the stream according to the comparator provided in its argument.
Learn to use Stream sorted() method to sort a stream of elements in their natural order and also according to the provided Comparator.
Java examples of chained predicates and to perform ‘logical AND‘ and ‘logical OR‘ operations and collect the elements into a list.
Java 8 predicate negate() returns a predicate that is the logical negation of this predicate. Java 11 introduced not() which is also the same.
Learn to use Stream.filter(Predicate) method to traverse all the elements and filter all items which match a given condition through Predicate argument. 1. Stream filter() Method The stream() method syntax is as follows: Predicate is a functional interface and represents the condition to filter out the non-matching elements from the …
Learn to use Stream.forEachOrdered(Consumer action) method to traverse all the elements and performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order.
Learn to use Stream forEach(Consumer action) method to traverse all the elements of stream and performs an action for each element of this stream.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.