Home Java 8 - IntStream
Post
Cancel

Java 8 - IntStream

Java 8 - IntStream

Java 8 introduces a new concept called Streams, which make it easy to perform bulk operations on collections of data. IntStream is a special type of stream that deals with integer values. It is defined in the java.util.stream package and it provides various operations to perform on integer values.

Creating an IntStream

There are several ways to create an IntStream:

  • Using IntStream.range() method:
1
2
IntStream.range(0, 10) // creates a stream of integers from 0 to 9

  • Using IntStream.rangeClosed() method: ```java IntStream.rangeClosed(0, 10) // creates a stream of integers from 0 to 10
1
2
3
4
5
* Using IntStream.of() method:
```java
IntStream.of(1, 2, 3, 4, 5) // creates a stream of integers with the given values

IntStream Operations

IntStream provides various operations to perform on integer values:

  • forEach()

This method performs an action on each element of the stream.

1
IntStream.range(1, 5).forEach(System.out::println); // prints 1 2 3 4
  • map():

This method transforms each element of the stream to another value.

1
IntStream.range(1, 5).map(i -> i * i).forEach(System.out::println); // prints 1 4 9 16
  • filter():

This method filters the elements of the stream based on some condition.

1
IntStream.range(1, 10).filter(i -> i % 2 == 0).forEach(System.out::println); // prints 2 4 6 8
  • sum():

This method returns the sum of all elements in the stream.

1
int sum = IntStream.range(1, 5).sum(); // sum equals to 10
  • average():

This method returns the average of all elements in the stream.

1
double avg = IntStream.range(1, 5).average().getAsDouble(); // avg equals to 2.5
  • count():

This method returns the count of all elements in the stream.

1
long count = IntStream.range(1, 5).count(); // count equals to 4
  • max():

This method returns the maximum element in the stream.

1
int max = IntStream.range(1, 5).max().getAsInt(); // max equals to 4
  • min():

This method returns the minimum element in the stream.

1
int min = IntStream.range(1, 5).min().getAsInt(); // min equals to 1
  • distinct():

This method returns a stream with distinct elements.

1
 IntStream.of(1, 2, 3, 2, 1).distinct().forEach(System.out::println); // prints 1 2 3
  • sorted():

This method returns a stream with sorted elements.

1
IntStream.of(3, 1, 4, 2, 5).sorted().forEach(System.out::println); // prints 1 2 3 4 5
  • limit():

This method returns a stream with a maximum number of elements.

1
IntStream.range(1, 10).limit(5).forEach(System.out::println); // prints 1 2 3 4 5
  • skip():

This method returns a stream with the first n elements removed.

1
 IntStream.range(1, 10).skip(5).forEach(System.out::println); // prints 6 7 8

Conclusion

In conclusion, Java 8 provides a powerful and efficient way to work with streams of data using the IntStream interface and its operations. The ability to perform operations on primitive integer values helps to improve the performance of our code and makes it more readable.

This post is licensed under CC BY 4.0 by the author.