Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the applications of Optional container classes, the new features of Java8?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the applications of the new features of Java8 Optional container class". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the applications of Java8 new features Optional container class".

1.Optional container class

The Optional class (java.util.Optional) is a container class that represents the existence or non-existence of a value. Originally, null is used to indicate that a value does not exist, but now Optional can better express this concept. And null pointer exceptions can be avoided.

Common methods:

Optional.of (T t): create an instance of Optional

Optional.empty (): create an empty Optional instance

Optional.ofNullable (T t): if t is not null, create an Optional instance; otherwise, create an empty instance

IsPresent (): determines whether a value is included

OrElse (T t): returns a value if the calling object contains a value, otherwise t

OrElseGet (Supplier s): if the calling object contains a value, return that value, otherwise return the value obtained by s

Map (Function f): return Optional.empty () if there is a value to process it and return the processed Optional.

FlatMap (Function mapper): similar to map, the return value must be Optional

two。 Application example

The following code examples are given in the order of the methods listed above. First, you need an Employee class to use as a test.

Package com.szh.java8; import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor; / * * * / @ Data@NoArgsConstructor@AllArgsConstructorpublic class Employee {private Integer id; private String name; private Integer age; private Double salary;} @ Test public void test1 () {Optional op = Optional.of (new Employee ()); Employee employee = op.get (); System.out.println (employee);}

Test public void test2 () {Optional op = Optional.empty (); System.out.println (op.get ());}

@ Test public void test3 () {Optional op = Optional.ofNullable (new Employee (1001, "Zhang Qiling", 18Magne666.66); System.out.println (op.get ());}

@ Test public void test4 () {Optional op = Optional.of (new Employee ()); if (op.isPresent ()) {System.out.println (op.get ());}}

@ Test public void test5 () {Optional op1 = Optional.ofNullable (null); Employee employee1 = op1.orElse (new Employee (1001, "Zhang Qiling", 18Magne666.66)); System.out.println (employee1); Optional op2 = Optional.ofNullable (new Employee ()); Employee employee2 = op2.orElse (new Employee (1001, "Zhang Qiling", 18jue 6666.66)); System.out.println (employee2);}

@ Test public void test6 () {Optional op1 = Optional.ofNullable (null); Employee employee1 = op1.orElseGet (()-> new Employee (1001, "Zhang Qiling", 18Power666.66); System.out.println (employee1); Optional op2 = Optional.ofNullable (new Employee ()); Employee employee2 = op2.orElseGet (()-> new Employee (1001, "Zhang Qiling", 1866.66)) System.out.println (employee2);}

@ Test public void test7 () {Optional op1 = Optional.of (new Employee (1001, "Zhang Qiling", 18MC 666.66); Optional op2 = op1.map (Employee::getName); System.out.println (op2.get ()); Optional op3 = op1.flatMap ((e)-> Optional.of (e.getName (); System.out.println (op3.get ());}

Thank you for your reading, these are the contents of "what are the applications of the new features of Java8 Optional container class?" after the study of this article, I believe you have a deeper understanding of the application of the new features of Java8 Optional container class, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report