YouTip LogoYouTip

Java9 Stream Api Improvements

# Java 9 Improved Stream API [![Image 3: Java 9 New Features](#) Java 9 New Features](#) The improved Stream API in Java 9 adds some convenience methods that make stream processing easier and allow for writing complex queries using collectors. Java 9 added several new methods to Stream: `dropWhile`, `takeWhile`, `ofNullable`, and a new overloaded version of the `iterate` method. ### takeWhile Method **Syntax** default Stream takeWhile(Predicate predicate) The `takeWhile()` method takes a predicate as an argument and returns a subset of the given Stream until the predicate first returns false. If the first value does not satisfy the predicate condition, it returns an empty Stream. In an ordered Stream, `takeWhile` returns as many elements as possible from the beginning; in an unordered Stream, `takeWhile` returns a subset of elements from the beginning that satisfy the Predicate. ## Example import java.util.stream.Stream; public class Tester{public static void main(String[]args){Stream.of("a","b","c","","e","f").takeWhile(s->!s.isEmpty()) .forEach(System.out::print); }} In the above example, the `takeWhile` method stops looping and outputting when it encounters an empty string. The execution output is: abc ### dropWhile Method **Syntax** default Stream dropWhile(Predicate predicate) The `dropWhile` method works opposite to `takeWhile`. It takes a predicate as an argument and returns a subset of the given Stream only after the predicate first returns false. ## Example import java.util.stream.Stream; public class Tester{public static void main(String[]args){Stream.of("a","b","c","","e","f").dropWhile(s-> !s.isEmpty()) .forEach(System.out::print); }} In the above example, the `dropWhile` method starts looping and outputting when it encounters an empty string. The execution output is: ef ### iterate Method **Syntax** static Stream iterate(T seed, Predicate hasNext, UnaryOperator next) This method allows creating a sequential (possibly infinite) stream with an initial seed value and iteratively applying the specified next function. Iteration stops when the specified `hasNext` predicate returns false. ## Example java.util.stream.IntStream; public class Tester{public static void main(String[]args){IntStream.iterate(3, x ->xx+ 3).forEach(System.out::println); }} The execution output is: 369 ### ofNullable Method **Syntax** static Stream ofNullable(T t) The `ofNullable` method can prevent NullPointerExceptions by allowing you to check the stream to avoid null values. If the specified element is non-null, it obtains the element and generates a single-element stream; if the element is null, it returns an empty stream. ## Example import java.util.stream.Stream; public class Tester{public static void main(String[]args){long count = Stream.ofNullable(100).count(); System.out.println(count); count = Stream.ofNullable(null).count(); System.out.println(count); }} The execution output is: 10 [![Image 4: Java 9 New Features](#) Java 9 New Features](#)
← Java9 Optional Class ImprovemeJava9 Multirelease Jar β†’