小编r.l*_*y91的帖子

面试题-如何使用Optional.of()或Stream.of()来减少代码

我最近进入了最后一轮面试。

在面试中,他们有一次要求我通过以下代码展示我的 Java 8 知识。Optional.of()他们要求我使用或 来减少以下代码Stream.of()。我完全被冻结了,我只使用过列表上的流,不知道如何使用可选方法。我没有专门因为这个原因得到这份工作,因为他们说我对java8的理解还不够好。有人可以告诉我他们在找什么吗?

概括

我被特别要求用or减少这些2行:Optional.of()Stream.of()

gameDto = gameplay.playRandomGame(gameDto);
repo.updateTotals(gameDto.getResult());
Run Code Online (Sandbox Code Playgroud)

一些上下文的总体片段:

@Service("gameService")
public class GameServiceImpl implements GameService{
    
    @Autowired
    private SessionInMemoryRegistry sessionRegistry;
    
    @Autowired
    private GameInMemoryRepo repo;
    
    @Autowired
    private GamePlay gameplay;

    @Override
    public ResponseDto addGameToSession(GameDto gameDto) {
        gameDto = gameplay.playRandomGame(gameDto);
        repo.updateTotals(gameDto.getResult());
        return sessionRegistry.addGameSession(gameDto.getSessionId(), gameDto.getPlayer1Choice(), gameDto.getPlayer2Choice(), gameDto.getResult());
    }
}
Run Code Online (Sandbox Code Playgroud)

java lambda java-stream option-type

0
推荐指数
1
解决办法
105
查看次数

标签 统计

java ×1

java-stream ×1

lambda ×1

option-type ×1