Tag: java 8 streams

Java 8 Stream distinct Sample

Java 8 Stream distinct operation will return a stream consisting of the distinct elements of the Stream. The distinct() operation is an intermediate operation, which means it return a new stream. So distinct() creates a new Stream that contains

Java 8 Stream peek Sample

Java 8 Stream peek operation will return a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. The peek() operation is an intermediate operation,

Java 8 Stream flatMap Sample

Java 8 Stream flatMap operation will return a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. In simpler

Java 8 Iterate Over HashMap

You have a few options in dealing with HashMap with the introduction of Java 8 Streams and Lambda Expressions. Prior to Java 8, iterating over HashMap was exclusively done with a for loop. The Stream.forEach() method has replaced that

Java 8 Stream toArray Sample

Java 8 Stream toArray operation will return an array containing the elements of the Stream. Th Steam can be Integer, String, Char, or any other Java primitive data type. The Stream.toArray() has the following syntax: Object toArray() toArray() Stream

Java 8 Stream findAny Sample

Java 8 Stream findAny operation, which is very intuitive, will find any element from a given Stream. To be used when you are looking for an element without concern for the encounter order. The behavior of findAny() is nondeterministic,

Java 8 Stream findFirst Sample

Java 8 Stream findFirst operation is pretty straight forward in that it finds and returns the first element in the Stream. The Stream.findFirst() operation states that if the stream has no encounter order, any element may be returned. The

Java 8 Stream filter Sample

Java 8 Streams filter operation returns a stream consisting of the elements of this stream that match the given predicate. The Stream.filter() operation will transform the data of one stream into another stream using whatever condition you have set.