当前位置:网站首页>【Optional】1. Map and ifpresent 2 Ofnullable and orelse
【Optional】1. Map and ifpresent 2 Ofnullable and orelse
2022-06-12 14:36:00 【Understand the principle + good code skills】
1) Example 1
package org.example.testOptional;
import lombok.Data;
import lombok.ToString;
import java.util.Optional;
public class Main {
public static Optional<A> getOpA() {
return Optional.ofNullable(null);
}
public static void main(String[] args) {
getOpA().map(a -> a.getB())
.ifPresent(a -> {
System.out.println(a);
});
}
}
@Data
@ToString
class A {
private String desc = "aaa";
private int b = 1;
}
2) Example 2
package org.example.testOptional;
import java.util.Optional;
public class UnitObject {
private int id = 1;
public UnitObject() {
}
public UnitObject getOwner() {
return null;
}
public UnitObject getOwnerOrSelf() {
// Find the owner , If the master can't find it, he is himself
UnitObject o = Optional.ofNullable(getOwner()).orElse(this);
return o;
}
@Override
public String toString() {
return "Main2{" +
"id=" + id +
'}';
}
public static void main(String[] args) {
UnitObject unitObject = new UnitObject().getOwnerOrSelf();
System.out.println(unitObject);
}
}
/*
Main2{id=1}
*/
边栏推荐
- Notepad common settings
- MobileOne: 移动端仅需1ms的高性能骨干,你值得拥有!
- Autofac初学(1)
- C secret script Chapter 3 (detailed explanation of string function) (Section 1)
- webdriver入门
- Soft test (VI) Chrome browser installation selenium IDE
- 完美收官|详解 Go 分布式链路追踪实现原理
- junit测试套件方法整理(方法二不太好用)
- Two months' experience in C language
- 【MySQL】数据库基本操作
猜你喜欢

用游戏来讲序列化与反序列化机制

New technology: efficient self-monitoring visual pre training, local occlusion no longer need to worry!

Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge

What is automatic bidding? What are its advantages?

Recursive summary of learning function

华为设备配置BGP AS号替换

How to realize the bidding strategy that pays more attention to transformation in the company's operation Google sem

Copy word content to excel and automatically divide it into multiple columns

The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)

How to package QT program learning records with inno setup
随机推荐
NetCore结合CAP事件总线实现分布式事务——入门(1)
[ROC] aspriseocr C # English, Digital identification (not Chinese)
MobileOne: 移动端仅需1ms的高性能骨干,你值得拥有!
[wechat applet] 6.1 applet configuration file
Server concurrency - note 1
PMP敏捷知识点
2022版Redis数据删除策略
Junit测试中常用的断言
Socket model of punctual atom stm32f429 core board
Nesting of C language annotations
Player practice 19 xaudio turn on audio
Two methods of implementing asynchronous calling function with QT
Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?
Player actual combat 22 to solve the problems of flower screen and Caton
Common DOS commands
[wechat applet] 4 Introduction to wechat developer tools
Recursive summary of learning function
面向优化科学研究领域的软件包
Crawler problem set (I) record
Getting started alicloud haas510 open board DTU (version 2.0) --510-as