Java 8 IntStream operations with examples
IntStream is a specialized Stream for manipulating int values.
In this article, we will see some examples of IntStream operations.
Creating an IntStream
IntStream static method of() accepts an int array as argument and returns an IntStream.
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; IntStream intStream = IntStream.of(numbers);
Iterating over IntStream using forEach
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; IntStream.of(numbers).forEach(System.out::println);
Output :
2
4
1
7
5
10
3
Iterating over IntStream using iterator()
We can also use the iterator() method to obtain a PrimitiveIterator that can be used to iterate over the elements.
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; PrimitiveIterator.OfInt intIterator = IntStream.of(numbers).iterator(); while (intIterator.hasNext()) { System.out.println(intIterator.nextInt()); }
Output :
2
4
1
7
5
10
3
IntStream map() operation
The below example uses map() operation on an IntStream to find square of each of the numbers.
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; IntStream.of(numbers).map(n-> n*n) .forEach(System.out::println);
Output :
4
16
1
49
25
100
9
IntStream reduce() operation
In this example, we use IntStream reduce() operation to calculate sum of the numbers.
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; int sum = IntStream.of(numbers).reduce(0, (x,y) -> x+y); System.out.println("Sum of numbers = " + sum);
Output :
Sum of numbers = 32
IntStream filter() operation
Following example use filter() operation on IntStream to filter even numbers.
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; IntStream.of(numbers).filter(n -> n%2==0).forEach(System.out::println);
Output :
2
4
10
Sorting an IntStream
We can sort an IntStream using the sorted() method.
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; IntStream.of(numbers).sorted().forEach(System.out::println);
Output :
1
2
3
4
5
7
10
IntStream min(), max(), sum(), average() functions
Following examples demonstrates min(), max(), sum(), average() InStream functions.
int[] numbers = { 2, 4, 1, 7, 5, 10, 3 }; System.out.println("Min = " + IntStream.of(numbers).min().getAsInt()); System.out.println("Max = " + IntStream.of(numbers).max().getAsInt()); System.out.printf("Average = %.2f%n" , IntStream.of(numbers).average().getAsDouble()); System.out.println("Sum = " + IntStream.of(numbers).sum());
Output :
Min = 1
Max = 10
Average = 4.57
Sum = 32
Creating sequentially order IntStream using range() and rangeClosed() operations
The range(int startInclusive, int endExclusive) method returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.
Similarly, rangeClosed(int startInclusive, int endInclusive) method returns a sequential ordered IntStream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1.
System.out.println("Sum of 1 to 9 = " + IntStream.range(1, 10).sum()); System.out.println("Sum of 1 to 10 = " + IntStream.rangeClosed(1, 10).sum());
Output :
Sum of 1 to 9 = 45
Sum of 1 to 10 = 55
Convert an Intstream to int[] using toArray() method
Here is an example of toArray() method that converts an IntStream to an int[].
int[] arr1 = IntStream.of(1,2,3,4,5).toArray(); System.out.println(Arrays.toString(arr1));
Output :
[1, 2, 3, 4, 5]
Getting an IntStream from a Stream using mapToInt() function
Stream class’s mapToInt() returns an IntStream consisting of the results of applying the given function to the elements of this stream.
Lets see an examples of getting an IntStream of IDs from Stream of Person objects.
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public String toString() { return "Name : " + getName() + ", Age : " + getAge(); } }
Employee emp1 = new Employee(1,"Ajay"); Employee emp2 = new Employee(2,"Bob"); Employee emp3 = new Employee(3,"Chetan"); List<Employee> empList = new ArrayList<Employee>(); empList.add(emp1); empList.add(emp2); empList.add(emp3); empList.stream().mapToInt(p -> p.getId()).forEach(System.out::println);
Output :
1
2
3
IntStream mapToObj() operation
IntStream has a method called mapToObj that takes a function that maps an int to an Object.
IntStream.of(1, 2, 3, 4, 5).mapToObj(i -> { return Integer.toString(i * i); }).forEach(System.out::println);
Output :
1
4
9
16
25
Parallel IntStream
A parallel stream is allowed to operate on elements in any order and operate on multiple elements at the same time.
By default, streams in Java are sequential.We can use parallel streams by calling stream.parallel().
IntStream.of(1, 2, 3, 4, 5).parallel().mapToObj(i -> { return Integer.toString(i * i); }).forEach(System.out::println);
Output :
9
1
25
4
16
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post