Primitive Type Streams in Java 8

1. Introduction

The Stream API was one of the key features added in Java 8.

Briefly, the API allows us to process collections and other sequences of elements – conveniently and more efficiently – by providing a declarative API.

2. Primitive Streams

Streams primarily work with collections of objects and not primitive types.

Fortunately, to provide a way to work with the three most used primitive types – int, long and double – the standard library includes three primitive-specialized implementations: IntStreamLongStream, and DoubleStream.

Primitive streams are limited mainly because of boxing overhead and because creating specialized streams for other primitives isn’t’ that useful in many cases.

3. Arithmetic Operations

Let’s start with a few interesting methods for heavily used arithmetic operations such as minmaxsum, and average:

int[] integers = new int[] {20, 98, 12, 7, 35};
int min = Arrays.stream(integers)
  .min()
  .getAsInt(); // returns 7

Let’s now step through the code snippet above to understand what’s going on.

We created our IntStream by using java.util.Arrays.stream(int[]) and then used the min() method to get the lowest integer as java.util.OptionalInt and finally called getAsInt() to get the int value.

Another way to create an IntStream is using IntStream.of(int…). The max() method will return the greatest integer:

int max = IntStream.of(20, 98, 12, 7, 35)
  .max()
  .getAsInt(); // returns 98

Next – to get the sum of integers we just call the sum() method and we don’t need to use getAsInt() since it already returns the result as an int value:

int sum = IntStream.of(20, 98, 12, 7, 35).sum(); // returns 172

We invoke the average() method to get the average of integer values and as we can see, we should use getAsDouble() as it returns a value of type double.

double avg = IntStream.of(20, 98, 12, 7, 35)
  .average()
  .getAsDouble(); // returns 34.4

4. Range

We can also create an IntStream based on a range:

int sum = IntStream.range(1, 10)
  .sum(); // returns 45
int sum = IntStream.rangeClosed(1, 10)
  .sum(); // returns 55

As the code snippet above shows there are two ways to create a range of integer values range() and rangeClosed().

The difference is that the end of range() is exclusive while it is inclusive in rangeClosed().

Range methods are only available for IntStream and LongStream.

We can use range as a fancy form of a for-each loop:

IntStream.rangeClosed(1, 5)
  .forEach(System.out::println);

What’s good at using them as a for-each loop replacement is that we can also take advantage of the parallel execution:

IntStream.rangeClosed(1, 5)
  .parallel()
  .forEach(System.out::println);

As helpful as these fancy loops are it’s still better to use the traditional for-loops instead of the functional one for simple iterations because of simplicity, readability, and performance in some cases.

5. Boxing and Unboxing

There’re times when we need to convert primitive values to their wrapper equivalents.

In those cases, we can use the boxed() method:

List<Integer> evenInts = IntStream.rangeClosed(1, 10)
  .filter(i -> i % 2 == 0)
  .boxed()
  .collect(Collectors.toList());

We can also convert from the wrapper class stream to the primitive stream:

// returns 78
int sum = Arrays.asList(33,45)
  .stream()
  .mapToInt(i -> i)
  .sum();

We can always use mapToXxx and flatMapToXxx methods to create primitive streams.

6. Conclusion

Java Streams is a very powerful addition to the language. We’ve barely scratched the surface of primitive streams here, but, as you can already use them to be productive.

And, as always, code samples can be found over on GitHub.

Related posts:

Spring Cloud – Securing Services
Java Program to add two large numbers using Linked List
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Implement Interpolation Search Algorithm
Java Program to Generate Random Numbers Using Multiply with Carry Method
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
String Joiner trong Java 8
Jackson Date
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Java Program to Represent Graph Using Linked List
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
A Guide to the ViewResolver in Spring MVC
New Features in Java 15
Java Program to implement Dynamic Array
ExecutorService – Waiting for Threads to Finish
Migrating from JUnit 4 to JUnit 5
The Spring @Controller and @RestController Annotations
Mapping a Dynamic JSON Object with Jackson
Java Program to Check for balanced parenthesis by using Stacks
So sánh ArrayList và Vector trong Java
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Spring RestTemplate Error Handling
Guide to PriorityBlockingQueue in Java
The Registration Process With Spring Security
Java Program to Implement Range Tree
Generating Random Numbers in a Range in Java
The XOR Operator in Java
Java Program to Create the Prufer Code for a Tree
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Implement Affine Cipher
Spring 5 and Servlet 4 – The PushBuilder