Primitive streams example in java8 (IntStream, LongStream, DoubleStream)

Java stream library have streams specifically for primitive types like int, long, double etc.

  • With primitive streams, values are directly stored in primitives (not in wrapper classes like Integer, Long or Double).
  • The rest of primitives like char, short, byte etc will make use existing streams like IntStream, LongStream and DoubleStream.

1. Create IntStream using IntStream.of

IntStream intStream = IntStream.of(1, 3, 5, 7);
intStream.forEach(System.out::println);
//generate the output of 1, 3, 5, 7  (each digit in new line)

2. Create IntStream using range & rangeClosed method

IntStream stream1to9 = IntStream.range(1, 10);
//generate number from 1 to 9
 
IntStream stream1to10 = IntStream.rangeClosed(1, 10);
//generate number from 1 to 10

3. Create IntStream using iterate method

IntStream iterateStream1to10 = IntStream.iterate(1, step -> step + 1).limit(10);
// prints 1 to 10
iterateStream1to10.forEach(System.out::println);
//generate number from 1 to 10

4. Convert IntStream to Stream<Integer>:

IntStream has boxed method to box the primitives to corresponding wrapper classes.

Stream IntegerStream = IntStream.range(1, 10).boxed();

5. Generate statistics of IntStream :

IntStream stream1to10 = IntStream.rangeClosed(1, 10);
double avg = stream1to10.average().getAsDouble();              
System.out.println("Avg :"+ avg);
IntStream stream1to9 = IntStream.range(1, 10);
IntSummaryStatistics statistics = stream1to9.summaryStatistics();
int min = statistics.getMin();
int max = statistics.getMax();
avg = statistics.getAverage();
System.out.println("Min:"+min + " max:"+max + " avg:"+avg);
//output : Min:1 max:9 avg:5.0

6. Program – primitive streams examples java8 (IntStream etc)

package org.learn.PrimitiveStreams;
 
import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;
 
public class App {
    public static void main(String[] args) {
 
        IntStream intStream = IntStream.of(1, 3, 5, 7);
        //Print 1 3 5 7
        intStream.forEach(System.out::println);
         
        IntStream stream1to9 = IntStream.range(1, 10);
        //Prints 1 to 9
        //stream1to9.forEach(System.out::println);
        IntStream stream1to10 = IntStream.rangeClosed(1, 10);
        // prints 1 to 10
        //stream1to10.forEach(System.out::println);    
        IntStream iterateStream1to10 = IntStream.iterate(1, step -> step + 1).limit(10);
        // prints 1 to 10
        //iterateStream1to10.forEach(System.out::println);
         
        //Get avg of stream
        double avg = stream1to10.average().getAsDouble();              
        System.out.println("Avg :"+ avg);
         
        //Generate statistics of int stream
        IntSummaryStatistics statistics = stream1to9.summaryStatistics();
        int min = statistics.getMin();
        int max = statistics.getMax();
        avg = statistics.getAverage();
        System.out.println("Min:"+min + " max:"+max + " avg:"+avg);
         
    }
}

Output – Primitive streams examples java8 (IntSummaryStatistics etc)

1
3
5
7
Avg :5.5
Min:1 max:9 avg:5.0