Java8我用的最开心肯定是stream,一旦习惯了写法确实代码简洁了不少(当然除了能读懂,还要学会stream的debug用法,否则写的时候很爽,但是一出现问题就特别痛苦)。
实际上还忽略了一个好的工具:Optional
A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value.
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() (return a default value if value not present) and ifPresent() (execute a block of code if the value is present).
This is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances of Optional may have unpredictable results and should be avoided.
Since:
1.8
通过上面的截图,我们大概能知道Optional是一个对象容器,这个对象可以是空的,里面的方法也都是围绕这个可能为空的对象的值存在与否,后续处理和判断。
看起来很没用,就是一个可以为空的容器,但是你要结合你自己平时的代码问题去看,“”