Streams – IntStream class

IntStream class is part of Java 8 Numeric Streams addition, and it represents int primitive values in a Stream.

IntStream is part of the java.util.stream package and implements AutoCloseable and BaseStream interfaces.


How to create an IntStream in Java?

There are several ways of creating an IntStream:

1. IntStream.of() method

Using the of() method we can specify the values ​​we want the IntStream to contain.
There are 2 types of the of() method, one which takes only one element and the one that can take more than one.

Syntax:

IntStream.of(5);
IntStream.of(1, 2, 3);


Here, the first statement will create an IntStream that will hold only one element (5).
The second statement creates an IntStream that holds values 1, 2, 3.


2. IntStream.range() and IntStream.rangeClosed() methods

With range() and rangeClosed() methods, we define a range of ints that we want to create a stream of.
The rangeClosed() function includes the ending int, while range() excludes it.

Syntax:

IntStream.range(1, 3);  
IntStream.rangeClosed(1, 3);  


Here, the range() function will return an IntStream of values 1 and 2.
The rangeClosed() function returns the IntStream of 1, 2 and 3.


3. IntStream.iterate() method

We use the iterate() function to create infinite streams. However, we can limit it by using the limit() function.

Syntax:

IntStream.iterate(0, i -> i + 2).limit(10);


Here, we created an IntStream that contains values: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18].


4. IntStream.generate() method

Returns an infinite sequential unordered stream where the provided IntSupplier generates each element. This is suitable for generating constant streams, streams of random elements, etc.

Syntax:

IntStream.generate(() -> (int) (Math.random() * 5)).limit(10);


The above code will return an IntStream of 10 random numbers multiplied by 5.


Examples

Example 1:
Iterate over an IntStream.
IntStream supports the forEach() method for iterating over the elements, just like the regular Stream.

class Test {

  public static void main(String[] args) {
    IntStream intStream = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    intStream.forEach(item -> System.out.print(item + " "));
  }
}

Output: 1 2 3 4 5 6 7 8 9 10


Example 2:
Convert an IntStream to a List.
Since Collections in Java can not store the primitive values directly, we need to use the boxed() method of IntStream class to convert the primitive type to the Wrapper class.

class Test {

  public static void main(String[] args) {
    List<Integer> integers = IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
            .boxed()
            .collect(Collectors.toList());

    System.out.println(integers);
  }
}

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


Example 3:
Convert an IntStream to an array using the toArray() function:

class Test {

  public static void main(String[] args) {
    int[] array = IntStream.of(1, 2, 3, 4, 5).toArray();
    Arrays.stream(array).forEach(item -> System.out.print(item + " "));
  }
}

Output: 1 2 3 4 5


Example 4:
Filter the IntStream using the filter() function:

class Test {

  public static void main(String[] args) {
    IntStream.range(1, 10)
            .filter(i -> i % 2 == 0)
            .forEach(item -> System.out.print(item + " "));
  }
}

Output: 2 4 6 8


That’s it!

 

Leave a Reply

Your email address will not be published.