使用java 8可选, 有没有办法编写这行代码:
Bar bar = fooOpt.isPresent() ? new Bar(fooOpt.get().getX()) : null;
类似的东西:
Bar bar = fooOpt.ifPresent(f -> new Bar(f.getX()), null)
答案 0 :(得分:11)
也许您正在寻找:
for g in file:
name = g.replace('| ', '|\t').
假设:
Optional<Foo> fooOpt = ...
Bar bar = fooOpt.map(foo -> new Bar(foo.getX()))
.orElse(null);