Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Stream peek(...) method

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(...)

like image 645
Eugene Mamaev Avatar asked Mar 01 '19 13:03

Eugene Mamaev


People also ask

How do you use peek in stream?

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.

What does the Peek () method do when should you use it?

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.

What is difference between Peek and foreach?

(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.

What does the method stream peek consumer super T action describe?

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 ...

What is stream Peek method in Java?

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.

How to get the first element in a Kotlin stream?

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.

What is the difference between Kotlin and Java 8 stream?

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.

How to convert list to lazy sequence in Kotlin?

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:


2 Answers

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".

like image 115
s1m0nw1 Avatar answered Oct 08 '22 11:10

s1m0nw1


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()

like image 45
Mr.Q Avatar answered Oct 08 '22 12:10

Mr.Q