Introduction
In Java, IntStream
is a sequence of primitive int elements supporting sequential and parallel aggregate operations. It is part of the java.util.stream
package and provides a convenient way to work with sequences of int values.
Table of Contents
- What is
IntStream
? - Creating an
IntStream
- Common Operations
- Examples of
IntStream
- Real-World Use Case
- Conclusion
1. What is IntStream?
IntStream
is a specialized stream for working with int values, offering methods for processing elements, performing calculations, and generating statistics.
2. Creating an IntStream
You can create an IntStream
in several ways:
- From an array:
IntStream.of(int... values)
- Using
Arrays.stream(int[] array)
- From a range:
IntStream.range(start, end)
orIntStream.rangeClosed(start, end)
- Using
IntStream.generate()
orIntStream.iterate()
3. Common Operations
Some common operations on IntStream
include:
filter()
: Filters elements based on a condition.map()
: Transforms elements.sum()
: Computes the sum of the elements.average()
: Calculates the average of the elements.min()
/max()
: Finds the minimum or maximum element.sorted()
: Sorts the elements.forEach()
: Performs an action for each element.distinct()
: Removes duplicate elements.limit()
: Limits the stream to a certain number of elements.reduce()
: Performs a reduction on the elements.
4. Examples of IntStream
Example 1: Creating and Summing an IntStream
This example demonstrates how to create an IntStream
from a set of int values and calculate their sum.
import java.util.stream.IntStream;
public class SumExample {
public static void main(String[] args) {
int sum = IntStream.of(1, 2, 3, 4, 5)
.sum();
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 15
Example 2: Filtering and Averaging
In this example, we filter the stream to include only even numbers and then calculate the average of the remaining values.
import java.util.OptionalDouble;
import java.util.stream.IntStream;
public class FilterAverageExample {
public static void main(String[] args) {
OptionalDouble average = IntStream.of(1, 2, 3, 4, 5)
.filter(n -> n % 2 == 0)
.average();
System.out.println("Average of Even Numbers: " + average.orElse(0.0));
}
}
Output:
Average of Even Numbers: 3.0
Example 3: Mapping and Sorting
This example shows how to map each element of an IntStream
to its square and then sort the resulting values.
import java.util.stream.IntStream;
public class MapSortExample {
public static void main(String[] args) {
IntStream.of(3, 1, 4, 2)
.map(n -> n * n)
.sorted()
.forEach(System.out::println);
}
}
Output:
1
4
9
16
Example 4: Using distinct()
This example demonstrates how to remove duplicate elements from an IntStream
.
import java.util.stream.IntStream;
public class DistinctExample {
public static void main(String[] args) {
IntStream.of(1, 2, 2, 3, 3, 3, 4, 5)
.distinct()
.forEach(System.out::println);
}
}
Output:
1
2
3
4
5
Example 5: Using limit()
This example shows how to limit the stream to the first three elements.
import java.util.stream.IntStream;
public class LimitExample {
public static void main(String[] args) {
IntStream.range(1, 10)
.limit(3)
.forEach(System.out::println);
}
}
Output:
1
2
3
Example 6: Using reduce()
This example demonstrates how to use reduce()
to calculate the product of elements.
import java.util.stream.IntStream;
public class ReduceExample {
public static void main(String[] args) {
int product = IntStream.of(1, 2, 3, 4, 5)
.reduce(1, (a, b) -> a * b);
System.out.println("Product: " + product);
}
}
Output:
Product: 120
5. Real-World Use Case: Calculating Factorial
This example illustrates how to calculate the factorial of a number using IntStream
.
import java.util.stream.IntStream;
public class FactorialExample {
public static void main(String[] args) {
int number = 5;
int factorial = IntStream.rangeClosed(1, number)
.reduce(1, (a, b) -> a * b);
System.out.println("Factorial of " + number + ": " + factorial);
}
}
Output:
Factorial of 5: 120
Conclusion
IntStream
is used in Java for processing sequences of int values. It provides a range of operations for filtering, transforming, and aggregating data, making it particularly useful in numerical and computational tasks. Using IntStream
can lead to cleaner and more efficient code, especially in data processing contexts.
Comments
Post a Comment
Leave Comment