Statement lambda can be replaced with expression lambda

sdfsd picture sdfsd · Sep 15, 2017 · Viewed 38.7k times · Source

I do user and invitation validation using the Optional facility

@DeleteMapping("/friends/{username}")
public
HttpEntity<Boolean> removeFriend(
        @ApiParam(value = "The user's name", required = true) @PathVariable String username
) {
    Long fromId = authorizationService.getUserId();

    return userService.findByUsername(username)
            .map(user -> {
                 return friendshipService.findFriendship(fromId, user.getId())
                        .map(friendship -> {
                            friendshipService.removeFriendship(friendship);

                            friendship.setToId(friendship.getFromId());
                            friendship.setFromId(friendship.getToId());

                            friendshipService.removeFriendship(friendship);

                            return ResponseEntity.ok(true);
                        }).orElseGet(() -> ResponseEntity.notFound().build());
            }).orElseThrow(() -> new ResourceNotFoundException("User not found"));

However, IntelliJ is colouring my grey return, But when I remove the return, it highlights to me that there is no return.

Could someone explain how it works and what is it all about?

Answer

Seelenvirtuose picture Seelenvirtuose · Sep 15, 2017

Your statement lambda

param -> { return expression; }

can be changed to an expression lambda:

param -> expression

Simple, isn't it? Note, that the curly brackets and the semicolon need to be removed.