Java 8 IntStream, LongStream and DoubleStream Example

By Arvind Rai, December 31, 2014
In this page we are providing the example of Java 8 IntStream, LongStream and DoubleStream. IntStream, LongStream and DoubleStream are the specialization of primitive int stream, primitive long stream and primitive double stream respectively. These interfaces have many useful methods. They are initialized by using static method provided in the class.

IntStream

java.util.stream.IntStream is a sequence of primitive integer values. The aggregate operations like max and average can be performed using sequential and parallel operations.
rangeClosed(a,b): The values from a to be are considered by incrementing 1.
range(a,b) : Values from a to b-1 are considered.
sum: Calculates the sum of values.
sorted: Values are sorted.
Find the example of IntStream.
IntStreamDemo.java
package com.concretepage;
import java.util.stream.IntStream;
public class IntStreamDemo {
	public static void main(String[] args) {
		System.out.println("--Using IntStream.rangeClosed--");
		IntStream.rangeClosed(13, 15).map(n->n*n).forEach(s->System.out.print(s +" "));
		System.out.println("\n--Using IntStream.range--");
		IntStream.range(13,15).map(n->n*n).forEach(s->System.out.print(s +" "));
		System.out.println("\n--Sum of range 1 to 10--");
		System.out.print(IntStream.rangeClosed(1,10).sum());
		System.out.println("\n--Sorted number--");
		IntStream.of(13,4,15,2,8).sorted().forEach(s->System.out.print(s +" "));
	}
}
Find the output.
--Using IntStream.rangeClosed--
169 196 225 
--Using IntStream.range--
169 196 
--Sum of range 1 to 10--
55
--Sorted number--
2 4 8 13 15  

LongStream

java.util.stream.LongStream is a sequence of primitive long values and aggregate operations are performed using sequential and parallel operations. Methods are same like IntStream.
LongStreamDemo.java
package com.concretepage;
import java.util.stream.LongStream;
public class LongStreamDemo {
	public static void main(String[] args) {
		System.out.println("--Using LongStream.rangeClosed--");
		LongStream.rangeClosed(13, 15).map(n->n*n).forEach(s->System.out.print(s +" "));
		System.out.println("\n--Using LongStream.range--");
		LongStream.range(13,15).map(n->n*n).forEach(s->System.out.print(s +" "));
		System.out.println("\n--Sum of range 1 to 10--");
		System.out.print(LongStream.rangeClosed(1,10).sum());
		System.out.println("\n--Sorted number--");
		LongStream.of(13,4,15,2,8).sorted().forEach(s->System.out.print(s +" "));
	}
} 
Find the output.
--Using LongStream.rangeClosed--
169 196 225 
--Using LongStream.range--
169 196 
--Sum of range 1 to 10--
55
--Sorted number--
2 4 8 13 15  

DoubleStream

java.util.stream.DoubleStream is a sequence of primitive double values and aggregate operations are performed using sequential and parallel operations.
average: Calculates the average.
max: Finds the max value.
Find the example of DoubleStream.
package com.concretepage;
import java.util.function.DoublePredicate;
import java.util.stream.DoubleStream;
public class DoubleStreamDemo {
	public static void main(String[] args) {
		System.out.println("--Using DoubleStream.of--");
		DoubleStream.of(5.33,2.34,5.32,2.31,3.51).map(d->d*1.5).forEach(s->System.out.print(s +" "));
		System.out.println("\n--Using DoubleStream.average--");
		double val = DoubleStream.of(12.1,11.2,13.3).average().getAsDouble();
		System.out.println(val);
		System.out.println("--Using DoubleStream.max--");
		val = DoubleStream.of(12.1,11.2,13.3).max().getAsDouble();
		System.out.println(val);
		System.out.println("--Using DoubleStream.filter--");
		DoublePredicate range = d -> d > 12.11 && d < 12.99;		
		DoubleStream.of(12.1,11.2,12.3).filter(range).forEach(d->System.out.print(d));
	}
} 
Find the output.
--Using DoubleStream.of--
7.995 3.51 7.98 3.465 5.265 
--Using DoubleStream.average--
12.200000000000001
--Using DoubleStream.max--
13.3
--Using DoubleStream.filter--
12.3 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us