Primitive streams
Primitive streams were introduced in Java 8, to take advantage of primitive data types in Java while using Streams (again, Streams are basically from Java, and Kotlin just adds a few extension functions to the Streams API). IntStream
, LongStream
, and DoubleStream
are part of those primitive Streams.
These primitive streams work similarly to the normal Stream with some added features of the primitive data types.
So, let's take an example; have a look at the following program:
fun main(args: Array<String>) { val intStream = IntStream.range(1,10) val result = intStream.sum() println("The sum of elements is $result") }
So, we created an IntStream
value with the IntStream.range()
function, the range
function takes two integers as the starting and ending point and creates a Stream ranging from the specified integers, with both included. We then calculated the sum and printed it. The program seems quite easy, and credit goes to IntStream
obviously...