So after some reading I\'ve seen that
if (optional.isPresent()) {
//do smth
}
is not the preferred way to use Optional (http://www.ora
If you can incorporate the name into the Car constructor, then you can write this:
car = optional.map(id -> getCar(id))
.orElseGet(() -> new Car(carName));
If you must call the setter separately from your constructor, you would end up with something like this:
car = optional.map(id -> getCar(id))
.orElseGet(() -> {
Car c = new Car();
c.setName(carName);
return c;
});