📘 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
- 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.
Comments
Post a Comment
Leave Comment