Optional
<h5>Option非空判断</h5>
<pre><code>//相当于命令式编程 判断依次判断逻辑
Optional.ofNullable(one).map(x -> x.getUsername()).map(x -> x.toUpperCase()+"BBBB").map(x -> x.toLowerCase()).ifPresent(x -> System.out.println(x));</code></pre>
<h5>非空对象</h5>
<pre><code>
/**
* 返回非空对象
* @param one
* @return
*/
public User getUser_(User one) {
return Optional.ofNullable(one).orElseGet(() ->new User());
}
/**
* 返回非空对象
* @param one
* @return
*/
public User getUser(User one) {
return Optional.ofNullable(one).orElse(new User());
}</code></pre>
<h5>list判空并处理逻辑</h5>
<pre><code>Optional.ofNullable(list).ifPresent(x ->{
x.forEach(y ->{
String start = y.getStartEdition();
String end = y.getEndEdition();
start=start==null?"0":start;
end=end==null?"0":end;
if(edition.compareTo(start)>=0||edition.compareTo(end)<=0){
result.add(y.getContext());
}
});
});</code></pre>
<h5>判断字符串为空</h5>
<pre><code>Object o="wwww";
String string="";
string=Optional.ofNullable(o).orElse("").toString();
System.out.println("::::"+string);</code></pre>