Primitive Type Streams
Because Stream operates on object references, it can’t operate directly on primitive types, such as double, float, long, short, char, byte, and boolean. To handle primitive type streams, the stream library has specialized types IntStream, LongStream, and DoubleStream that store primitive values directly, without using wrappers. If you want to store int, use an IntStream, for double, use a DoubleStream, and for long, use a LongStream. These streams all extend BaseStream and have capabilities similar to Stream except that they operate on primitive types rather than reference types.
There are a number of ways to create primitive type streams.
Using IntStream.of( ) Method to Create a IntStream
To create an IntStream using IntStream, call the IntStream.of( ) methods. It has two variants:
Using IntStream.range( ) Method to Create a IntStream
In addition, IntStream and LongStream have static methods range() and rangeClosed() that generate integer ranges with step size one.
Using Arrays.stream( ) Method to Create a IntStream
To create an IntStream using Arrays, call the Arrays.stream( ) methods. It has two variants for IntStream:
Making Infinite IntStream
The IntStream interface has two static methods for making infinite IntStreams. The generate( ) and iterate( ):
String to IntStream
The CharSequence interface has methods codePoints() and chars() that yield an IntStream of the Unicode codes of the characters or of the code units in the UTF-16 encoding. Note that String implements the CharSequence interface.
The limit() and skip() Methods
The call of instance method limit(n) returns a new stream that ends after n elements (or when the original stream ends, if it is shorter). This method is particularly useful for cutting infinite streams down to size.
The call of instance method skip() returns a stream consisting of the remaining elements of this stream after discarding the first specified number of elements of the stream.
Reduction Operations
The IntStream contains many terminal operations that return one value by combining the contents of a stream. These operations are called reduction operations. Many reduction operations perform a specific task. Some of these are sum, average, max, and min that return the sum, average, maximum, and minimum. These methods are not defined for object streams.
Program
import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.DoubleStream; public class Javaapp { public static void main(String[] args) { int intarray[] = {10,20,30,40,50,60,70,80,90,100}; IntStream istm1 = Arrays.stream(intarray); System.out.print("istm1-->"); istm1.forEach((e)->System.out.print(e+",")); System.out.println(); System.out.println("Average-->"+Arrays.stream(intarray).average().getAsDouble()); double d=5; DoubleStream dstm2 = DoubleStream.iterate(d,(e)->e+=d).limit(5); System.out.print("dstm2-->"); dstm2.forEach((e)->System.out.print(e+",")); System.out.println(); System.out.println("Sum-->"+DoubleStream.iterate(d,(e)->e+=d).limit(5).sum()); } }