これはJava 8でのオプションの有効な使用方法ですか?Java 8でのオプションの有効な使用方法
class Foo {
private final Integer id;
private final String name;
private final String countryCode;
Foo(final Integer id, final String name, final String countryCode) {
this.id = id;
this.name = name;
this. countryCode = countryCode;
}
public String getName() {
return name;
}
public String getCountryCode() {
return countryCode;
}
}
class FooSrv {
private Optional<Foo> getFoo(final Integer id) {
return id != null && id == 1 ? Optional.of(new Foo(id, "Bar", "US")) : Optional.empty();
}
private boolean isFooValid(final Integer id) {
return getFoo(id)
.filter(f -> "Bar".equals(f.getName()) && "US".equals(f.getCountryCode()))
.map(f -> true)
.orElse(false);
}
}
質問:なぜ-1ですか? –
'.map(f - > true)。orElse(false);'は '.isPresent()'と言っても分かりません。 –
それは意味しませんでした。もし存在していれば、他のオブジェクトを返すということは、別のオブジェクトを返すということです。ブールはおそらく私の例では最善の選択ではなかった –