HomeSearch

Java Filter Example: findFirst, IntStream

This Java example uses the filter method on an IntStream. It calls findFirst to get the first element.
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.

Lambda: The lambda in this program returns true if the element is greater than or equal to 40. Otherwise it returns false.

Lambdas

FindFirst: This returns an OptionalInt, which may be the first element in the stream (if an element even exists).

GetAsInt: This method returns the int represented by the OptionalInt instance. It must be called only if isPresent returns true.

Java program that uses filter, findFirst 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()); } } } Output 40
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.

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.If

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.

Java program that causes NoSuchElementException 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()); } } Output Exception in thread "main" java.util.NoSuchElementException: No value present at java.util.OptionalInt.getAsInt(Unknown Source) at Program.main(Program.java:13)
Summary. Filter receives a lambda that specifies which elements to keep. So it filters out all non-matching elements in a stream.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls