- takeWhile(Predicate Interface)
- dropWhile(Predicate Interface)
- iterate()
- ofNullable()
Learn about Java 8 Streams at https://www.javaguides.net/p/java-8-stream-api-tutorial.html
Stream takeWhile(Predicate Interface)
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);
}
}
abc
a
Stream dropWhile(Predicate Interface) method
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);
}
}
bcef
ef
Stream iterate() method
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);
}
}
[1, 2, 3, 4, 5]
Stream ofNullable() method
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());
}
}
1
0
Related Java 9 Posts
- Java 9 Private Methods in Interface with Examples - Learn how to use private methods in interface with examples.
- Java 9 List.of() Method - Create Immutable List Example - In this post, I show you how to create an immutable list using Java 9 provided List.of() static factory method.
- Java 9 Set.of() Method - Create Immutable Set Example - In this post, I show you how to create an immutable Set using Java 9 provided Set.of() static factory method.
- Java 9 Map.ofEntries() Method - Create Immutable Map Example - In this post, I show you how to create an immutable Map using Java 9 provided Map.ofEntries() static factory method.
- Java 9 - Stream API Improvements with Examples - In this article, we will learn what are the Stream API improvements made in Java 9 with an example.
- Java 9 - Optional Class Improvements with Examples - Learn Optional class improvements with examples.
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment