JAva 8 Optional優雅非空判断
4270 ワード
JAva 8 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 opt = Optional.ofNullable(str);
opt.ifPresent(n -> {
System.out.println(n);
});
List strs = new ArrayList();
strs.add("1");
strs.add("2");
Optional> opts = Optional.ofNullable(strs);
opts.ifPresent(opt -> {
opt.forEach(n -> {
System.out.println(n);
});
});
}
}