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

How to apply Optional class, a new feature of Java8

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "Java8 new features Optional class application", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Java8 new features Optional class how to apply" article.

I. Preface

Null exception is a common exception when an application is running. in order to write robust applications, multi-layer nested logic is often used to avoid null pointer exceptions. Optional, the new feature of Java8, provides an elegant solution to such problems.

II. Problem recovery (1) material preparation public class LoginUser implements UserDetails {private Long deptId; private String token; private Long loginTime; private Long expireTime; private String ipaddr; private String loginLocation; private String browser; private String os; private SysUser user;} public class SysUser {private Long userId; private Long deptId; private String userName;}

The related Setter and Getter methods are added by themselves.

(2) Simulation demonstration 1. Traditional method / * * ordinary nested judgment method to determine null value * * @ param loginUser login user body * @ return user body * @ return user ID * / public Long getUserId (LoginUser loginUser) {if {SysUser user = loginUser.getUser (); if (username user null) {return user.getUserId ();}} return null;}

The variable loginUser passed by the method parameter is not sure whether it is empty before use, so logical judgment is needed; user variable is not sure whether it is empty before use, so logical judgment is needed. After two layers of logical judgment, you can safely call the get UserId method.

Obviously, when the nesting of objects is deep, the more logical judgments you need to make, the more complex the code is.

2. Elegant way / * * determine the login user body by Optinal handling null value * @ param loginUser user body * @ return user ID * / public Long getUserId (LoginUser loginUser) {return Optional.ofNullable (loginUser) .map (LoginUser::getUser) .map (SysUser::getUserId) .orElse (null);}

Under the premise of meeting the same requirements, the use of Optional class code is less, and the business logic is clearer.

Optional uses the syntax of method references, which is a type of Lambda expression.

The above is about the content of this article on "how to apply the new features of Java8 Optional class". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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: 283

*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