Showing posts with label Optional. Show all posts
Showing posts with label Optional. Show all posts

Sunday, April 12, 2020

Java 8 Optional orElseThrow() Example | Throw Exception in Optional in Java 8

1. introduction


In this tutorial, We'll learn how to throw an exception if the option is empty. Optional API orElseThrow() method returns value from Optional if present. Otherwise, it will throw the exception created by the Supplier.

2. Syntax


public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
                                    throws X extends Throwable


Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new, ArithmeticException::new

Java 8 Optional orElseGet() Example

1. Introduction

In this tutorial, We'll learn java 8 Optional API orElseGet() method examples and where to use.

2. Syntax

[lock]
public T orElseGet(Supplier<? extends T> other)

Return the value if present, otherwise invoke other and return the result of that invocation. This method takes the Supplier Functional Interface. [/lock]

if the Supplier is null, it throws NullPointerException.


Wednesday, September 11, 2019

Java 8 Optional ifPresent() - Working Example

1. Overview


In this tutorial, We'll learn how to perform an action if a value is present in Optional. Java 8 Optional ifPresent() does the job for us.

Instead of directly getting the value using get() method, first, it checks the condition value != null.

java-8-optional-ifpresent


The old way is done using the isPresent() method as below. But, ifPresent is much simplified than isPresent().

Wednesday, August 7, 2019

Java 8 Optional flatMap() Method Example

1.Overview


In this tutorial, We'll learn how to wrap all nested Optional values into a Direct Map instead of having hierarchies.
You get a better understanding once you see the example.

You will be learning today syntax, examples and its internal implementation part.

This is part of our Optional Method series course.

API Description:

If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional. This method is similar to map(Function), but the mapping function is one whose result is already an Optional, and if invoked, flatMap does not wrap it within an additional Optional.

Sunday, August 4, 2019

Java 8 Optional equals() Method Example

1. Overview

In this tutorial, We'll learn how to compare the Optional value with another value or object. Optional is a java 8 new class and introduced in java.util package.

First, we'll understand the syntax and its meaning. Next, equals() example program and its internal implementation.

optional-equals


Optional class All Methods

API Note: 

Indicates whether some other object is "equal to" this Optional value. The other object is considered equal if it is also an Optional and; both instances have no value present or; the present values are "equal to" each other via equals().

Thumb rule is that the passed object also must be Optional object. Otherwise, this method will return false.

Thursday, August 1, 2019

Java 8 Optional filter() Method Example

1. Overview

In this tutorial, We'll discuss how to use Predicate with Optional class. The Java 8 optional class has a method filter() which takes Predicate as an argument.

Optional is a class and it is in java.util package. Optional is declared as final in its source code. Because no other classes can be inherited and to stop overriding the behavior.

Java 8 Optional filter


API Note: If a value is present, and the value matches the given predicate, returns an Optional describing the value, otherwise returns an empty Optional.

Wednesday, July 31, 2019

Java 8 Optional ofNullable() Method Example

1. Overview


In this tutorial, We'll learn ofNullable() example on how to create new Java 8 Optional object for any value or null value. Optional is part of java.util package.

Java 8 Optional ofNullable() Method Example


API Note: Returns an Optional describing the given value, if non-null, otherwise returns an empty Optional.

Tuesday, July 30, 2019

Java 8 Optional of() Method Example

1. Overview


In this tutorial, We'll learn how to create an Optional object for any value in Java 8. This is a very useful method to create an instance of Optional. Optional is a new class and introduced part of java.util package.

In last tutorial, We've discussed Optional.empty() method.

Let us take a look at Syntax, Example program on of() method.

API note: Returns an Optional describing the given non-null value. If null value, it throws NullPointerException.

Java 8 Optional of() Method Example


Monday, July 29, 2019

Java 8 Optional empty() Method Examples

1. Overview


In this tutorial, We'll learn how to create an empty Optional instance using Java 8 empty() method. Let us take at the Java 8 Optional empty() example program.
Before that let us take a look at the syntax first and then next its usage.

empty() method is used to create an empty Optional object. That means no value is present for this Optional. Optional is a final class.



optional-empty