Code: Select all
public class Clazz {
final String someAttribute;
public String getSomeAttribute() {
return someAttribute;
}
public void setSomeAttribute(String someAttribute) {
this.someAttribute = someAttribute;
}
}
< /code>
Und ich habe das Attribut in der Klasse bedingt festgelegt. < /p>
Clazz someObject = new Clasz();
if(someCondition) {
someObject.setSomeAttribute(“someString”);
} else {
// someAttribute is not set, so it is null.
}
< /code>
Wenn ich versuche, das Attribut über eine MAP () -Transformation zu erhalten und es in einer ternären Operation zu verwenden Obwohl es sein kann. < /p>
Optional.ofNullable(someObject)
.map(Clazz::getSomeAttribute)
.map(someAttribute->Objects.notNull(someAttribute)) ? “string1” : string2)
.orElse(“”);
Danke im Voraus!