What is the best alternative in Kotlin to java.util.stream.Stream<>.peek(...)?
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#peek-java.util.function.Consumer-
Seems there are no alternative intermediate operations:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.streams/index.html
I found only terminating forEach(...)
Java Stream peek() method returns a new Stream consisting of all the elements from the original Stream after applying a given Consumer action. Note that the peek() method is an intermediate Stream operation so, to process the Stream elements through peek() , we must use a terminal operation.
peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack. Parameters: The method does not take any parameters.
(1): using stream. foreach, you can also change the internal property value of each item in the list, etc., but you need to perform "secondary flow processing" to get the smallest item in the list (filter according to age). (2): stream. peek can get the smallest item directly compared with stream.
In Java, Stream provides an powerful alternative to process data where here we will be discussing one of the very frequently used methods named peek() which being a consumer action basically returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as ...
Java Stream peek() method 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. Using Stream.peek() without any terminal operation does nothing.
For example, IntStream.generate will produce a potentially infinite Stream of integers. If we call findFirst () on it, we will get the first element, and not run into an infinite loop. In Kotlin, collections are eager, rather than lazy.
In Java 8, this operation is required to return a Stream instance, whereas in Kotlin it can return any collection type. This makes it easier to work with. In Java 8, the second line would need to be wrapped in Arrays.toStream () for this to work.
All collections in Kotlin can be converted to a lazy sequence using the asSequence () method. Using a Sequence instead of a List in the above example performs the same number of operations as in Java 8. 4. Java 8 Stream Operations In Java 8, Stream operations are broken down into two categories:
The Stream alternative in Kotlin is Sequences.
listOf(1, 2, 3, 4, 5)
.asSequence()
.filter { it < 3 }
.onEach { println("filtered $it") }
.map { it * 10 }
.forEach { println("final: $it") }
There's onEach
to do what peek
does.
Fun fact: Kotlin also wanted to call their sequences "Streams" before it was clear that Java would do the same, so they renamed it to "Sequences".
Firstly, unlike Java in Kotlin, you can perform stream processing (map/reduce operations) on any type of collection for example:
val list = ArrayList<Int>()
list.forEach { }
list.onEach { }
However the operations defined in this way are not lazily evaluated and if we need lazy evaluation by applying the method .asSequence()
and generate a stream from collection.
Finally to answer your question onEach()
is the equivalent of peek()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With