In the ever-evolving world of Java, the introduction of Java 8 brought with it a plethora of new features. Among these, the IntStream
stands out as a powerful tool designed specifically for operations on primitive int
values. Let's delve deep into the intricacies of IntStream
and explore its capabilities.
Introducing IntStream
IntStream
is a specialized version of the Stream
class tailored for primitive int
values. It offers both sequential and parallel aggregate operations, making it an indispensable tool when working with numeric data. Whether you're looking to count elements, find the minimum or maximum value, calculate the sum, or determine the average, IntStream
has got you covered.
Creating an IntStream
There are multiple ways to create an IntStream
:
- Using the
of()
method: This static factory method allows you to create anIntStream
from specific values.
IntStream iStream = IntStream.of(1, 2, 3, 4, 5, 6);
Using the range()
and rangeClosed()
methods: These methods generate sequences of integers. While range()
excludes the end value, rangeClosed()
includes it.
IntStream.range(10, 18); // Produces 10, 11, 12, 13, 14, 15, 16, 17
IntStream.rangeClosed(20, 25); // Produces 20, 21, 22, 23, 24, 25
Converting a Stream to IntStream: The mapToInt()
function is incredibly useful for this purpose, especially when converting numeric Strings to primitive integers.
List<String> listOfNumbers = Arrays.asList("41", "42", "43", "44");
IntStream fromMapping = listOfNumbers.stream().mapToInt(Integer::valueOf);
Key Methods of IntStream
- Aggregation Functions: These include
count()
,min()
,max()
,sum()
, andaverage()
. It's worth noting thatmax()
andmin()
return anOptionalInt
, so you might need to usegetAsInt()
to retrieve the actual integer value. - Sorting: The
sorted()
method sorts the values in the stream. - Filtering: Using the
filter()
method, you can filter out values based on certain conditions.
A Practical Example
Let's consider a scenario where we have a list of students, and we want to perform various operations on their marks:
class Student {
String name;
int marks;
public Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
public String getRollnumber() {
return name;
}
public int getMarks() {
return marks;
}
@Override
public String toString() {
return String.format("[%s, %d]", name, marks);
}
}
public class Test {
public static void main(String args[]) {
List<Student> candidates = new ArrayList<>();
candidates.add(new Student("John", 33));
candidates.add(new Student("Jack", 11));
candidates.add(new Student("Rick", 50));
candidates.add(new Student("Shane", 90));
int highestMarks = candidates.stream()
.mapToInt(s -> s.getMarks())
.max().getAsInt();
long numOfPassCandidates = candidates.stream()
.filter(c -> c.getMarks() >= 33)
.count();
double avgMarksOfPassedStudents = candidates.stream()
.filter(c -> c.getMarks() >= 33)
.mapToInt(c -> c.getMarks())
.average().getAsDouble();
}
}
In the above example, we've demonstrated how to use IntStream
to calculate the highest marks, the number of passed candidates, and the average marks of passed students.
Conclusion
Java 8's IntStream
is a powerful tool that simplifies operations on primitive integers. Its rich set of features, combined with its ease of use, makes it an essential addition to any Java developer's toolkit. Whether you're a seasoned developer or just starting out, mastering IntStream
will undoubtedly elevate your Java programming skills.