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

Friday, November 6, 2020

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.

Thursday, September 10, 2020

Java Optional as Return Type

1. Overview

In this article, you'll learn how to use Optional as a return type in java 8.  Let us explore the most useful methods when working with Optional objects.

And also where Option can not fit and the scenarios where you should not use Optional in java.

Optional is added in java 8 to make clean the code and to easy way to work with the null values.

Let us jump into the Optional return type and its most important methods.

We've already covered in-depth about Optional API

Java Optional as Return Type

Tuesday, September 8, 2020

Filtering a Stream of Optionals in Java (With Examples)

1. Introduction


In this article, You'll be learning how to remove or filter the non-empty values from Stream of Optionals from Optional API.

Usually, you work with Stream of Strings or Stream of custom objects. But, there are some cases where you frequently work with Optional values.

Java 8 has come up with a new Optional API which is to avoid the NullPointerExceptions and others as well. Actually, our real value will be wrapped into an Optional instance.

Filtering a Stream of Optionals in Java


Removing the empty or nullable values from the list can be done in multiple ways in Java 8 and Java 9 concepts.

Monday, August 24, 2020

Java 8 Optional Tutorial With Examples

1. Overview

In this tutorial, You will learn in-depth about Java 8 Optional Class methods and its usages.

Optional class is added to the java.util package. The intention of introducing this class in java 8 is mainly to check whether the value is present in the object or it is absent.

The object is to hold the set of values that means it contains the real values. So, such kind of object is called a Container.

The container object may contain a null or non-null value in it.

Java 8 Optional Tutorial With Examples

Monday, May 25, 2020

Optional ofNullable() method (With Examples)

1. Introduction


In this tutorial, We'll learn Optional 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.

Friday, May 22, 2020

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


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.

if the Supplier is null, it throws NullPointerException.

Filtering with Optional in Java 8

Java 8 Optional orElseGet() Example