Java 9: Stream API Improvements

In Java 9, following methods are added to Stream interface of stream package. These methods are default and static.

The takeWhile() Method

This method is included in Stream interface and used to collect the elements into a stream. It takes all the elements till the condition specified as predicate.

For example, we want to collect a few elements from a stream of elements; for this purpose, we can use takeWhile() method.

The following is the syntax of the method.

Example

Output

[20, 30, 40, 62] 20
30
40
62

The dropWhile() Method

This method is just opposite of takeWhile(). It drops the result when the specified condition occurs and takes rest of elements.

If stream is ordered then it returns a stream that contains remaining elements after dropping the elements. If the stream is unordered, it returns a stream that includes the remaining elements after dropping a subset of elements. The following is the syntax of the method.

Example

Output

[85, 21] 85
21

The ofNullable() Method

This method is used for null checking and returns either a sequential Stream of a single element or an empty Stream. The syntax of the method is given below.

Example

Output

12

The iterate() Method

This method is added into Java 9 that helps to iterate over a sequence of stream and takes three arguments. The first argument is called seed and used to initialize a stream, the second argument is called predicate and used to specify the condition while third argument is used to generate next elements.

The syntax of the method is given below.

Example

Output

1
2
3
4

This is similar to writing a for loop as below:

That’s all about Java 9 Stream API improvements.

Related Posts

  • 12 October

    Java 9 JShell tutorial

    Table of ContentsHello World Example Using JShellWhat can we do with JShell?How to start and stop JShell?Creating variable in JShellJShell as a CalculatorCreating Methods in JShellCreating Class in JShellExecuting Algebraic Expressions in JShellExecuting Conditional Statements in JShellJShell CommandsList the VariablesList the MethodsImport Java PackagesCheck history of commandsEdit code in JShell Edit PadLoad external Java code […]

  • 24 September

    Java 9 – Process API Improvements

    Table of ContentsThe Process ClassStart a new Process?MethodsExample to get Process IdExample to get Process InformationProcessHandle InterfaceMethodsExampleProcessHandle.Info Interface Methods In this post, we will see about Java 9 process API improvements. Java improved its Process API in Java 9 version that includes new methods for Process class and two new interfaces ProcessHandle and ProcessHandle.Info. These […]

  • 19 September

    Java try with resources

    Table of ContentsOlder approach to close the resourcesJava 7 try with resourcesSyntaxExampleJava 9 Try with resources ImprovementsFinally block with try with resourcesCreate Custom AutoCloseable Code In this post, we will see about Java try with resources Statement. Java try with resources is a feature of Java which was added into Java 7. It helps to […]

  • 19 September

    Java 9 – Javadoc Improvement

    Table of ContentsWhat is JavadocSimplified Doclet APISupport for HTML5Improved SearchSupport for Module SystemHow to create HTML Javadoc from Java source file.Run Javadoc Command Java has upgraded its Javadoc for better user interaction and user-experience. It includes a simplified search, HTML generator, Doclet API, etc. What is Javadoc The Javadoc is a tool like javac and […]

  • 19 September

    Java 9 – @SafeVarargs Annotation

    Table of [email protected] [email protected] with Final [email protected] with Static MethodJava 9 @SafeVarargs ImprovementWithout @SafeVarargs annotation In this post, we will see about @SafeVarargs Annotation in Java 9. The @SafeVarargs is an annotation that is used to perform safe operations. When a method takes variable arguments, then it may cause to unsafe operation, so the @SafeVarargs […]

  • 19 September

    Java 9 – Anonymous Inner classes and Diamond Operator

    Table of ContentsAnonymous class: Java 9 ImprovementExample: Anonymous class with diamond operator Type inference is a feature of Java that was introduced in Java 7. Now, Java compiler can infer type of a data automatically. For example, if we create an ArrayList then we use the code like: [crayon-62733e3fb88af921969993/] You can see that Java 7 […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.