Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statement lambda can be replaced with expression lambda

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?

like image 699
sdfsd Avatar asked Sep 15 '17 11:09

sdfsd


People also ask

Can be replaced with expression lambda?

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.

What is the difference between statement lambda and expression lambda?

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).

What is lambda expression?

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.


1 Answers

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.

like image 155
Seelenvirtuose Avatar answered Sep 30 '22 04:09

Seelenvirtuose