- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the new features added to Stream API in Java 9?
In Java 9, Oracle Corporation has added four useful new methods to Stream API. Those methods are iterate(), ofNullable(), takeWhile() and dropWhile().
iterate()
The iterate() can be used as stream version replacement of traditional for-loops. This method has improved by adding another parameter, the Predicate interface that allows us to stop these endless numbers based on conditions defined with the Predicate interface.
Example
import java.util.stream.Stream; public class StreamIterateMethodTest { public static void main(String[] args) { Stream.iterate(1, i -> i < 5, i -> i + 1).forEach(System.out::println); // iterate() } }
Output
1 2 3 4
ofNullable()
The ofNullable() method returns the stream object of an element if it is not null. Otherwise, it returns an empty stream.
Example
import java.util.stream.Stream; public class StreamOfNullableMethodTest { public static void main(String[] args) { String str = "TutorialsPoint"; Stream.ofNullable(str).forEach(System.out::println); // ofNullable() method } }
Output
TutorialsPoint
takeWhile()
The parameter passed to a takeWhile() method is a Predicate interface. This method takes an element of stream object from left to right until the condition of the Predicate object is no longer fulfilled.
Example
import java.util.stream.Stream; public class StreamTakeWhileMethodTest { public static void main(String[] args) { Stream.of(1, 2, 3, 4, 5) .takeWhile(i -> i < 5) // takeWhile() method .forEach(System.out::println); } }
Output
1 2 3 4
dropWhile()
The parameter passed to a dropWhile() method is also a Predicate interface. It is opposite to the takeWhile() method. This method passes each element in a stream object from left to right and ignores all elements that satisfy the condition. Once the condition is no longer fulfilled, it takes all remaining elements to return.
Example
import java.util.stream.Stream; public class StreamDropWhileMethodTest { public static void main(String[] args) { Stream.of(3, 2, 1, 4, 6, 7, 8, 9, 10) .dropWhile(i -> i < 5) // dropWhile() method .forEach(System.out::println); } }
Output
6 7 8 9 10
- Related Articles
- What are the new methods added to Process API in Java 9?
- What are new methods added to the String class in Java 9?
- What are the new features added in Python 3.10 version?
- What are the new methods added to an Optional class in Java 9?
- What are new methods have added to the Arrays class in Java 9?
- How to use the collect() method in Stream API in Java 9?
- What are the best New features added to iPhone and iPad with iOS12?
- Importance of iterate() method of Stream API in Java 9?\n
- How can we implement methods of Stream API in Java 9?
- What are the steps to execute Flow API in Java 9?
- What are the improvements in Process API in Java 9?
- What are the CompletableFuture API improvements in Java 9?\n
- What are the features that are added in MySQL 8.0?
- What are the main features and enhancements introduced in Java 9?
- What are the new features in MySQL 8.0
