Java流中的continue关键字的等价物?

Sau*_*mar 2 java java-8 java-stream

  for (final Prices ppr : prices) {
    if (!currency.getCode().equals(ppr.getCurrency().getCode())) {
      continue;
    }
    return ppr.getPrice();
  }
Run Code Online (Sandbox Code Playgroud)

上面的代码可以转换成Java流代码吗?我收到continue关键字错误...

Joh*_*ica 9

return prices.stream()
     .filter(ppr -> currency.getCode().equals(ppr.getCurrent().getCode()))
     .findFirst()
     .orElseThrow(NoSuchElementException::new);
Run Code Online (Sandbox Code Playgroud)

  • @george这是一个风格问题.我更喜欢避免使用`Optional.get()`,因为它隐藏了结果的可选性. (4认同)
  • 约翰在这里有一点意见.甚至讨论在Java 9中弃用Optionial.get().参见http://mail.openjdk.java.net/pipermail/core-libs-dev/2016-April/040484.html (2认同)