java8 Optional优雅非空判断
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class OptionalTest {
public static void main(String[] args) {
String str = null;
Optional<String> opt = Optional.ofNullable(str);
opt.ifPresent(n -> {
System.out.println(n);
});
List<String> strs = new ArrayList<String>();
strs.add("1");
strs.add("2");
Optional<List<String>> opts = Optional.ofNullable(strs);
opts.ifPresent(opt -> {
opt.forEach(n -> {
System.out.println(n);
});
});
}
}