TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java filter Example: findFirst, IntStream

Use the filter method on an IntStream. Call 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.

Lambda

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
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)
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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf