Java 11 Cookbook
上QQ阅读APP看书,第一时间看更新

A better way to work with nulls using Optional

In this recipe, you will learn how to use the java.util.Optional class for representing optional values instead of using null references. It was introduced in Java 8 and further enhanced in Java 9—where three more methods were added—or(), ifPresentOrElse(), and stream(). We will demonstrate all of them.