Java 9 - Stream API Improvements with Examples

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

In this article, we will learn what are the Stream API improvements made in Java 9 with an example.
Streams were introduced in Java to help developers perform aggregate operations from a sequence of objects. With Java 9, few more methods are added to make streams better.
Java 9 improvements in Stream API:
  • takeWhile(Predicate Interface)
  • dropWhile(Predicate Interface)
  • iterate()
  • ofNullable()
Let's learn each of these methods with examples.
Learn about Java 8 Streams at https://www.javaguides.net/p/java-8-stream-api-tutorial.html

Stream takeWhile(Predicate Interface)

The takeWhile() method takes all the values until the predicate returns false. It returns, in case of ordered stream, a stream consisting of the longest prefix of elements taken from this stream matching the given predicate.
Example:
package net.javaguides.corejava.java9; import java.util.stream.Stream; /**  * Demonstrates Java 9 Stream API Improvements  *   * @author Ramesh Fadatare  *  */ public class Java9StreamAPIExample { public static void main(String[] args) { Stream.of("a", "b", "c", "", "e", "f").takeWhile(s - > !s.isEmpty()) .forEach(System.out::print); System.out.println(); Stream.of("a", "", "c", "", "e", "f").takeWhile(s - > !s.isEmpty()) .forEach(System.out::print); } }
Output:
abc a

Stream dropWhile(Predicate Interface) method

The dropWhile() method throw away all the values at the start until the predicate returns true. It returns, in case of ordered stream, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements matching the given predicate.
Example:
package net.javaguides.corejava.java9; import java.util.stream.Stream; /**  * Demonstrates Java 9 Stream API Improvements  * @author Ramesh Fadatare  *  */ public class Java9StreamAPIExample { public static void main(String[] args) { Stream.of("a", "", "b", "c", "e", "f") .dropWhile(s - > !s.isEmpty()).forEach(System.out::print); System.out.println(); Stream.of("a", "b", "c", "", "e", "", "f") .dropWhile(s - > !s.isEmpty()).forEach(System.out::print); } }
Output:
bcef ef

Stream iterate() method

The iterate() method now has hasNext predicate as a parameter which stops the loop once hasNext predicate returns false.
Example:
package net.javaguides.corejava.java9; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /**  * Demonstrates Java 9 Stream API Improvements  *   * @author Ramesh Fadatare  *  */ public class Java9StreamAPIExample { public static void main(String[] args) { List < Integer > numbers = Stream.iterate(1, i - > i <= 5, i - > i + 1) .collect(Collectors.toList()); System.out.println(numbers); } }
Output:
[1, 2, 3, 4, 5]

Stream ofNullable() method

In Java 9, the ofNullable() method lets you create a single-element stream that wraps a value if not null, or is an empty stream otherwise.
Example:
package net.javaguides.corejava.java9; import java.util.stream.Stream; /**  * Demonstrates Java 9 Stream API Improvements  *   * @author Ramesh Fadatare  *  */ public class Java9StreamAPIExample { public static void main(String[] args) { Stream < String > stream = Stream.ofNullable("Ramesh"); System.out.println(stream.count()); stream = Stream.ofNullable(null); System.out.println(stream.count()); } }
Output:
1 0

Related Java 9 Posts

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare