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?
The Statement lambda can be replaced with expression lambda message appears when we use curly braces " {} " and semicolons " ; " in expression lambdas for implementing functional interfaces. We can do away with those in such a situation.
The difference between a statement and an expression lambda is that the statement lambda has a statement block on the right side of the lambda operator, whereas the expression lambda has only an expression (no return statement or curly braces, for example).
A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With