Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 语句lambda可以替换为表达式lambda_Java_Spring_Spring Mvc_Lambda_Optional - Fatal编程技术网

Java 语句lambda可以替换为表达式lambda

Java 语句lambda可以替换为表达式lambda,java,spring,spring-mvc,lambda,optional,Java,Spring,Spring Mvc,Lambda,Optional,我使用可选工具进行用户和邀请验证 @DeleteMapping("/friends/{username}") public HttpEntity<Boolean> removeFriend( @ApiParam(value = "The user's name", required = true) @PathVariable String username ) { Long fromId = authorizationService.getUserId();

我使用可选工具进行用户和邀请验证

@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"));
@DeleteMapping(“/friends/{username}”)
公众的
HttpEntity removeFriend(
@ApiParam(value=“用户名”,required=true)@PathVariable字符串用户名
) {
Long fromId=authorizationService.getUserId();
返回userService.findByUsername(用户名)
.map(用户->{
返回Friendservice.findFriendship(fromId,user.getId())
.map(友谊->{
友谊服务。解除友谊(友谊);
Frience.setToId(Frience.getFromId());
友谊.setFromId(友谊.getToId());
友谊服务。解除友谊(友谊);
返回ResponseEntity.ok(true);
}).orElseGet(()->ResponseEntity.notFound().build());
}).orelsetrow(()->new ResourceNotFoundException(“未找到用户”);
然而,
IntelliJ
正在给我的
return
上色,但是当我删除
return
时,它向我强调没有
return

有人能解释一下它是如何工作的吗?它是关于什么的

可以更改为表达式lambda:

很简单,不是吗?请注意,需要删除大括号和分号

有时我觉得离开办公室很有用 如果代码块足够长(我认为这可以提高可读性),则在它们所在的位置使用大括号

在Android Studio中,您可以在方法的开头使用
//noinspection CodeBlock2Expr
本地禁用警告,如下面的示例所示

//noinspection CodeBlock2Expr
button.setOnClickListener((View v) -> {
        //a long single method call...
});

语句lambda可以替换为表达式lambda
当我们使用大括号“
{}
”和分号“
”时,会出现一条消息在表达式中,lambda用于实现功能接口。我们可以取消这种情况下的lambda。也就是说,我个人认为,即使在单行功能接口实现中使用块lambda也是一种很好的编码实践,就像我们在单行
中为
中为
循环使用块lambda一样。

有效。谢谢s、 在您看来,哪种方法更适合lambda this或this?在这种情况下没有更好的方法。请随意操作。但对我来说,这两个代码段都不可读。这应该始终是编码的第一个标准。@Seelenvirtuose您介意给我们举一个可读代码段的示例吗?
param -> expression
//noinspection CodeBlock2Expr
button.setOnClickListener((View v) -> {
        //a long single method call...
});