IntStream is a part of Java 8 Stream API, specifically designed for primitive int values. It allows for functional programming operations on sequences of integers in a more efficient and concise way.
- Package:
java.util.stream - Primitive type:
int(as opposed toStream<Integer>)
| Method | Description |
|---|---|
range(start, end) |
Generates a stream of integers from start (inclusive) to end (exclusive). |
rangeClosed(start, end) |
Generates a stream of integers from start (inclusive) to end (inclusive). |
toArray() |
Converts the stream to an int[] array. |
forEach(action) |
Performs the given action for each element in the stream. |
map(mapper) |
Transforms each element using the provided function. |
sum() |
Computes and returns the sum of elements in the stream. |
average() |
Computes and returns the average of elements in the stream. |
filter(predicate) |
Filters elements based on the provided condition. |
collect(collector) |
Collects the elements into a collection (e.g., list). |
- Description: Generates a stream of integers starting from
start(inclusive) up toend(exclusive). - Example:
IntStream.range(1, 5); // Generates: 1, 2, 3, 4
- Description: Generates a stream of integers starting from
start(inclusive) toend(inclusive). - Example:
IntStream.rangeClosed(1, 5); // Generates: 1, 2, 3, 4, 5
- Description: Converts the
IntStreaminto anint[]array. - Example:
int[] arr = IntStream.range(1, 5).toArray(); // arr = [1, 2, 3, 4]
- Description: Applies the given action (lambda) to each element of the stream.
- Example:
IntStream.range(1, 5).forEach(System.out::println);
- Description: Transforms each element using the provided function.
- Example:
int[] squared = IntStream.range(1, 5) .map(x -> x * x) // Map to squares .toArray(); // squared = [1, 4, 9, 16]
- Description: Returns the sum of all the elements in the stream.
- Example:
int sum = IntStream.range(1, 5).sum(); // sum = 10 (1+2+3+4)
- Description: Returns the average of the elements in the stream as
OptionalDouble. - Example:
OptionalDouble avg = IntStream.range(1, 5).average(); // avg = 2.5
IntStream allows you to perform functional programming operations such as mapping, filtering, and reducing.
- Example:
int result = IntStream.range(1, 5) .filter(x -> x % 2 == 0) // Only even numbers .map(x -> x * x) // Square them .sum(); // Sum = 20 System.out.println(result); // Output: 20
IntStreamworks directly with the primitiveinttype, so there is no autoboxing (conversion toInteger).- This reduces memory overhead and leads to better performance compared to
Stream<Integer>.
import java.util.stream.IntStream;
public class IntStreamExample {
public static void main(String[] args) {
int start = 1; // Starting number
int end = 5; // Ending number (exclusive)
// Generate range from start to end-1, transform them and collect to an array
int[] arr = IntStream.range(start, end)
.map(x -> x * 2) // Double each number
.toArray(); // Convert to array
// Print the result
for (int num : arr) {
System.out.print(num + " "); // Output: 2 4 6 8
}
}
}| Method | Description |
|---|---|
IntStream.range(start, end) |
Creates a stream of integers from start to end - 1. |
IntStream.rangeClosed(start, end) |
Creates a stream of integers from start to end. |
toArray() |
Converts the stream to an int[] array. |
forEach(action) |
Performs the given action for each element in the stream. |
map(mapper) |
Transforms each element using the provided function. |
sum() |
Returns the sum of elements in the stream. |
average() |
Returns the average of elements in the stream. |
filter(predicate) |
Filters elements based on the provided condition. |
collect(collector) |
Collects the elements into a collection (e.g., list). |
IntStreamis ideal when you need to work with sequences of primitiveintvalues and want to perform operations in a functional programming style.- It is more efficient than using
Stream<Integer>due to its direct handling of primitive values, avoiding the overhead of boxing. - Provides a wide range of methods for transforming, filtering, and aggregating values.
- Java 8 Documentation: Stream API