
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
When to use the ofNullable() method of Stream in Java 9?n
The ofNullable() method is a static method of Stream class that returns a sequential Stream containing a single element if non-null, otherwise returns an empty. Java 9 has introduced this method to avoid NullPointerExceptions and also avoid null checks of streams. The main objective of using the ofNullable() method is to return an empty option if the value is null.
Syntax
static <T> Stream<T> ofNullable(T t)
Example-1
import java.util.stream.Stream; public class OfNullableMethodTest1 { public static void main(String args[]) { System.out.println("TutorialsPoint"); int count = (int) Stream.ofNullable(5000).count(); System.out.println(count); System.out.println("Tutorix"); count = (int) Stream.ofNullable(null).count(); System.out.println(count); } }
Output
TutorialsPoint 1 Tutorix 0
Example-2
import java.util.stream.Stream; public class OfNullableMethodTest2 { public static void main(String args[]) { String str = null; Stream.ofNullable(str).forEach(System.out::println); // prints nothing in the console str = "TutorialsPoint"; Stream.ofNullable(str).forEach(System.out::println); // prints TutorialsPoint } }
Output
TutorialsPoint
- Related Articles
- How to use the collect() method in Stream API in Java 9?
- When to use the readAllBytes() method of InputStream in Java 9?
- When to use the readNBytes() method of InputStream in Java 9?
- When to use the delayedExecutor() method of CompletableFuture in Java 9?
- When can we use StackWalker.getCallerClass() method in Java 9?
- How to use intermediate stream operations in JShell in Java 9?
- How to use terminal stream operations in JShell in Java 9?
- Importance of iterate() method of Stream API in Java 9?\n
- When to use fillInStackTrace() method in Java?
- What is the use of the toEpochSecond() method in Java 9?
- When to use the ServiceLoader class in a module in Java 9?
- What is the use of the Optional.stream() method in Java 9?\n
- When can we use the pack() method in Java?
- When can we use the getClass() method in Java?
- What are the new features added to Stream API in Java 9?

Advertisements