Reputation: 381
I have an if-then-else statement and I want to transform it to a ternary operator, but I do not know why I cannot do it. The code is the following:
public Movie create(NewMovieDTO newMovieDTO) {
Movie movieForSaving = NewMovieDTOToMovie.map(newMovieDTO);
List<Actor> actorsForSaving = new ArrayList<Actor>();
movieForSaving.getActors().forEach((actor) -> {
Optional<Actor> actorInDatabase = actorService
.findByNameAndSurname(actor.getName(), actor.getSurname());
if(actorInDatabase.isPresent()) {
actorForSaving.add(actorInDatabase.get());
} else {
actorForSaving.add(actor);
}
});
movieForSaving.setActors(actorForSaving);
return movieRepository.save(movieForSaving);
}
And the code with the ternary operator is:
public Movie create(NewMovieDTO newMovieDTO) {
Movie movieForSaving = NewMovieDTOToMovie.map(newMovieDTO);
List<Actor> actorsForSaving = new ArrayList<Actor>();
/*Line 1*/ movieForSaving.getActors().forEach((actor) -> {
Optional<Actor> actorInDatabase = actorService
.findByNameAndSurname(actor.getName(), actor.getSurname());
/*Line 2*/(actorInDatabase.isPresent()) ? actorForSaving.add(actorInDatabase.get()) : actorForSaving.add(actor);
/*Line 3*/});
movieForSaving.setActors(actorForSaving);
return movieRepository.save(movieForSaving);
}
The following errors are given by the IDE:
Line 1: The target type of this expression must be a functional interface
Line 2: Multiple markers at this line
- Syntax error, insert "AssignmentOperator Expression" to complete Assignment
- Syntax error, insert "}" to complete Block
- actorForSaving cannot be resolved to a variable
- Syntax error on token(s), misplaced construct(s)
- actorInDatabase cannot be resolved
- actorForSaving cannot be resolved
- Syntax error, insert ";" to complete Statement
Line 3: Syntax error on tokens, delete these tokens.
Is it possible to perform a ternary operator here or how can I solve it?
Thank you so much for your help!
Upvotes: 6
Views: 719
Reputation: 49606
actorForSaving.add(actorInDatabase.isPresent() ? actorInDatabase.get() : actor);
The ternary operator can't be a statement, it's an expression that returns something. In your case, actorInDatabase.isPresent() ? actorInDatabase.get() : actor
returns an Actor
.
Another good alternative would be using Optional#orElse
as -
actorForSaving.add(actorInDatabase.orElse(actor));
Upvotes: 8
Reputation: 109547
The error was already explained. Just the Streamy way to use all and Optional:
Movie movieForSaving = NewMovieDTOToMovie.map(newMovieDTO);
List<Actor> actorsForSaving = movieForSaving.getActors().stream()
.map(actor -> actorService.findByNameAndSurname(actor.getName(),
actor.getSurname()).orElse(actor))
.collect(Collectors.toList());
movieForSaving.setActors(actorForSaving);
Upvotes: 2
Reputation: 1074138
Your if
is just fine. But if you really want to use the conditional operator here, the way to do it is to do it within the argument list to add
:
movieForSaving.getActors().forEach((actor) -> {
Optional<Actor> actorInDatabase = actorService.findByNameAndSurname(actor.getName(), actor.getSurname());
actorForSaving.add(actorInDatabase.isPresent() ? actorInDatabase.get() : actor);
});
You may also be able to use orElse
. But your question seemed to be specifically about the conditional operator. (Which is a ternary operator — an operator accepting three operands — but not the ternary operator. Granted at the moment it's Java's only ternary operator, but in theory another could be added.)
Upvotes: 3