Java Streams

Primitive Streams

March 14, 2020

To provide support to work with primitives int, long and double, Java provides three primitive specialized implementations of Stream.

  • IntStream
  • LongStream
  • DoubleStream

This is how we can create primitive streams using these interfaces.

Creating Primitive Stream:

Using of() method | Arrays.stream() method

import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class PrimitiveStreams{
	public static void main(String[] args) {
		//using of() method
		IntStream intStream = IntStream.of(1,2,3,4,5);
		LongStream longStream = LongStream.of(1L,2L,3L,4L);

		//using Arrays.stream() method
		double[] arr = {1.2,2.3,3.4,4.5};
                DoubleStream doubleStream = Arrays.stream(arr);
	}
}

We use of() method with primitive streams to get the stream of corresponding primitive type, just like Stream.of() which is used to get the stream of objects.

Why Primitive Streams?

There are many methods in Primitive Streams which are used heavily for arithmetic operations like min, max, average, sum, etc.

If we want to perform any of these with object streams, we need to convert it to the primitive type stream. For example,

import java.util.List;

public class PrimitiveStreams {

	public static void main(String[] args) {

		//list of population of different cities
		List<Integer> list = List.of(1000000,120000,4527483,3687368,2343434);
		
		//to calculate total population,

		int totalPopulation = list.stream()
				.reduce(0,(e1,e2) -> e1+e2);
		
		System.out.println("Total Population is: "+totalPopulation);
	}

}
//Output
Total Population is: 11678285

Explanation: To get the sum of stream elements we can use reduce() method, but with that, we are unboxing the integer to int. So rather than doing that: we can use sum() method, that we can use with Number streams only. Consider the following example,

Transforming Stream to Primitive Stream:

//using sum() method after converting Integer stream to int stream
		int totalPopulation = list.stream()
				.mapToInt(e -> e)
				.sum();
//Output
Total Population is: 11678285

IntStream mapToInt(ToIntFunction<? super T> mapper) works exactly the same as map() method. map() returns Stream of objects. Like if the result is int, it will box it to Integer. Whereas mapToInt() will unbox the result to int.

Methods to transform stream to primitive streams:

  1. mapToDouble
  2. mapToLong
  3. mapToInt

Primitive streams are developed to avoid the cost of boxing and unboxing. We have them to get better performance!

Methods of Primitive Streams:

  • Methods for Arithmetic operations sum(), min(), max(), etc
//Finding max

int intArr = new int[] {10,34,28,65};
int max = Arrays.stream(intArr)
      .max();

System.out.println("max:" + max);
max : 65

Similarly, we can use other methods as well.

We can also create Primitive streams by using primitive based on some range too.

Range() and RangeClosed()

//Finding max

int max = IntStream.range(1,15);

int maxRangeClosed = IntStream.rangeClosed(1,15);

Difference between these two is, the end of range() is exclusive and of rangeClosed() is inclusive.

Also note: These are only available to IntStream and LongStream interfaces.