Filter. A stream contains many elements. With filter we remove elements that do not match a condition. The stream may become shorter.
Filter also returns a stream. And on this stream we can use a method like findFirst to get the first element. We must determine if any elements remain in the stream.
The example. First we use a standard int array and convert it into a stream with Arrays.stream. This is an IntStream instance. Next we use a lambda expression in the filter method.
Info The lambda in this program returns true if the element is greater than or equal to 40. Otherwise it returns false.
Also FindFirst() returns an OptionalInt, which may be the first element in the stream (if an element even exists).
Finally The getAsInt() method returns the int represented by the OptionalInt instance. It must be called only if isPresent returns true.
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class Program {
public static void main(String[] args) {
int[] array = { 10, 20, 30, 40, 50, 60 };
// Convert array to Stream.
IntStream stream = Arrays.stream(array);
// Filter outvalues less than 40.
OptionalInt result = stream.filter(value -> value >= 40)
.findFirst();
// If a result is present, display it as an int.
if (result.isPresent()) {
// This is the first value returned by the filter.
System.out.println(result.getAsInt());
}
}
}40
GetAsInt. Methods like getAsInt must be used in a guard. If we invoke getAsInt on an OptionalInt and no element exists, we receive a NoSuchElementException.
Here The filter call removes all elements from the IntStream. So the OptionalInt from findFirst has no value.
Result The NoSuchElementException is thrown. The isPresent method must be used after findFirst to be safe.
import java.util.Arrays;
import java.util.OptionalInt;
import java.util.stream.IntStream;
public class Program {
public static void main(String[] args) {
int[] array = { -100, -200 };
IntStream stream = Arrays.stream(array);
// This filters out all elements.
OptionalInt result = stream.filter(value -> value >= 0)
.findFirst();
System.out.println(result.getAsInt());
}
}Exception in thread "main" java.util.NoSuchElementException:
No value present
at java.util.OptionalInt.getAsInt(Unknown Source)
at Program.main(Program.java:13)
Filter argument. The filter() method receives an IntPredicate method on an IntStream. For other stream types, other predicates are used.
Tip The easiest way to specify arguments like IntStream is with lambda expressions.
A summary. Filter receives a lambda that specifies which elements to keep. So it filters out all non-matching elements in a stream.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 22, 2023 (edit).