Java IntStream class is an specialization of Stream
interface for int primitive. It represents an stream of primitive int-valued elements supporting sequential and parallel aggregate operations.
IntStream
is part of the java.util.stream
package and implements AutoCloseable
and BaseStream
interfaces.
Table of Contents 1. Creating IntStream 2. Foreach loop 3. IntStream range 4. Filter operation 5. IntStream to array 6. IntStream to list
1. Creating IntStream
There are several ways of creating an IntStream
.
1.1. IntStream.of()
This function returns a sequential ordered stream whose elements are the specified values.
It comes in two versions i.e. single element stream and multiple values stream
IntStream of(int t)
– Returns stream containing a single specified element.IntStream of(int... values)
– Returns stream containing specified all elements.
IntStream.of(10); //10 IntStream.of(1, 2, 3); //1,2,3
1.2. IntStream.range() and IntStream.rangeClosed()
These functions are discussed in more detail in later section (3). In this method, we provide a range of values (start and end of range) and then the function returns all int values from start value to end value.
IntStream.range(1, 5); //1,2,3,4,5
1.3. IntStream.iterate()
The iterator()
function is useful for creating infinite streams. Also, we can use this method to produce streams where values are increment by any other value than 1.
Given example produces first 10 even numbers starting from 0.
IntStream.iterate(0, i -> i + 2).limit(10); //0,2,4,6,8,10,12,14,16,18
1.4. IntStream.generate()
generate()
looks a lot like iterator()
, but differ by not calculating the int values by increment the previous value. Rather a IntSupplier is provided which is a functional interface is used to generate an infinite sequential unordered stream of int values.
Following example create stream of 10 random numbers and then print them in console.
IntStream stream = IntStream.generate(() -> { return (int)(Math.random() * 10000); }); stream.limit(10).forEach(System.out::println);
2. Foreach loop
To loop through the elements, stream support the forEach() operation. To replace simple for-loop using IntStream
, follow the same approach.
import java.util.stream.IntStream; public class ForEachExample { public static void main(String[] args) { //Simple for-loop for(int i=0; i < 5; i++) { doSomething(i); } //IntStream forEach IntStream.rangeClosed(0, 4) .forEach( ForEachExample::doSomething ); } private static void doSomething(int i) { System.out.println(i); } }
3. IntStream range
The IntStream
produced by range()
methods is a sequential ordered stream of int values which is equivalent sequence of increasing int values in a for-loop and value incremented by 1. This class supports two methods.
- range(int startInclusive, int endExclusive) – Returns a sequential ordered int stream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.
- rangeClosed(int startInclusive, int endInclusive) – Returns a sequential ordered int stream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1.
import java.util.stream.IntStream; public class RangeExample { public static void main(String[] args) { //Range IntStream stream = IntStream.range(5, 10); stream.forEach( System.out::println ); //5,6,7,8,9 //Closed Range IntStream closedRangeStream = IntStream.rangeClosed(5, 10); closedRangeStream.forEach( System.out::println ); //5,6,7,8,9,10 } }
4. Filter operation
We can apply filtering on int values produced by stream and use them in another function or collect them for further processing.
For example, we can iterate over int values and filter/collect all prime numbers upto a certain limit.
import java.util.List; import java.util.function.IntPredicate; import java.util.stream.Collectors; import java.util.stream.IntStream; public class FilterExample { public static void main(String[] args) { IntStream stream = IntStream.range(1, 100); List<Integer> primes = stream.filter(FilterExample::isPrime) .boxed() .collect(Collectors.toList()); System.out.println(primes); } public static boolean isPrime(int i) { IntPredicate isDivisible = index -> i % index == 0; return i > 1 && IntStream.range(2, i).noneMatch(isDivisible); } }
Program output.
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
5. IntStream to array
Use IntStream.toArray() method to convert from int stream to array.
int[] intArray = IntStream.of(1, 2, 3, 4, 5).toArray(); System.out.println(Arrays.toString(intArray)); Output: [1, 2, 3, 4, 5]
6. IntStream to list
Collections in Java can not store the primitive values directly. They can store only instances/objects.
Using boxed()
method of IntStream
, we can get stream of wrapper objects which can be collected by Collectors
methods.
List<Integer> ints = IntStream.of(1,2,3,4,5) .boxed() .collect(Collectors.toList()); System.out.println(ints); Output: [1, 2, 3, 4, 5]
Happy Learning !!
Ref : Java doc
Leave a Reply