更新时间:2023-10-05 14:44:34
因为它几乎与可选不匹配,也许您可能会重新考虑逻辑:
As it almost but not really matches Optional, maybe you might reconsider the logic:
Java 8的表现力有限:
Java 8 has a limited expressiveness:
Optional<Elem> element = ...
element.ifPresent(el -> System.out.println("Present " + el);
System.out.println(element.orElse(DEFAULT_ELEM));
此处地图
可能会限制元素上的视图:
Here the map
might restrict the view on the element:
element.map(el -> el.mySpecialView()).ifPresent(System.out::println);
Java 9:
element.ifPresentOrElse(el -> System.out.println("Present " + el,
() -> System.out.println("Not present"));
一般来说,两个分支是不对称的。
In general the two branches are asymmetric.