
- 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
What are the new methods added to an Optional class in Java 9?
An Optional class provides a container that may or may not contain a non-null value. This Optional class introduced in Java 8 to reduce the number of places in the code where a NullPointerException can be generated. Java 9 added three new methods to Optional class: or(), ifPresentOrElse() and stream() that help us to deal with default values.
Optional.or()
The or() method introduced in Java 9 and the parameter of this method is a functional interface Supplier. This method always gives us an Optional object that is not empty. If the Optional object is not empty, it returns the Optional object itself. Otherwise, it returns an Optional object that the Supplier creates.
Example
import java.io.IOException; import java.util.Optional; public class OptionalOrMethodTest { public static void main(String[] args) throws IOException { String str = null; Optional<String> opt = Optional.ofNullable(str); Optional<String> result = opt.or(() -> Optional.of("Adithya")); System.out.println(result); } }
Output
Optional[Adithya]
Optional.ifPresentOrElse()
The ifPresentOrElse() method introduced in Java 9. It is similar to the ifPresent() method with one difference is that we have added one more Runnable parameter. In case, if an Optional object is empty, the object of the Runnable interface can be executed.
Example
import java.util.Optional; public class OptionalIfPresentOrElseTest { public static void main(String[] args) { String str = null; Optional<String> opt = Optional.ofNullable(str); opt.ifPresentOrElse( x -> System.out.println(x), () -> System.out.println("No value")); } }
Output
No value
Optional.stream()
The Optional.stream() method is supported since Java 9. This method can be used to create a new stream object from an Optional object. If an Optional object contains a value, it returns the stream object containing that value.
Example
import java.io.IOException; import java.util.List; import java.util.Optional; import java.util.stream.Stream; public class OptionalStreamMethodTest { public static void main(String[] args) throws IOException { List<Optional<String>> list = List.of( Optional.of("Jai"), Optional.empty(), Optional.of("Adithya")); Stream<String> stream = list.stream().flatMap(Optional::stream); stream.forEach(System.out::println); } }
Output
Jai Adithya
- Related Articles
- What are new methods added to the String class in Java 9?
- What are new methods have added to the Arrays class in Java 9?
- What are the new methods added to Process API in Java 9?
- What are the new features added to Stream API in Java 9?
- How to get a stream from Optional class in Java 9?
- What are the rules for private methods in an interface in Java 9?
- What are the advantages of private methods in an interface in Java 9?
- Java 8 Optional Class
- Which factory methods have added for collections in Java 9?
- What are Class/Static methods in Java?\n
- What are the conditions for Collection factory methods in Java 9?
- What are the changes of class loaders in Java 9?
- What are the new features added in Python 3.10 version?
- How to define Optional Methods in the Swift Protocol?
- What kind of variables/methods defined in an interface in Java 9?
