Reputation: 16661
for (final Prices ppr : prices) {
if (!currency.getCode().equals(ppr.getCurrency().getCode())) {
continue;
}
return ppr.getPrice();
}
Can the code above be converted into Java stream code? I am getting an error with the continue
keyword...
Upvotes: 2
Views: 261
Reputation: 3221
Just to expand on the provided answer you can also do
return prices.stream()
.filter(ppr -> currency.getCode().equals(ppr.getCurrent().getCode()))
.findFirst()
.orElse(/* provide some default Price in case nothing is returned */);
instead of .findFirst().get() which returns the value which should not be empty. So it assumes there will be some data returned. With the orElse statement you can provide a default value in case nothing is returned.
Upvotes: 0
Reputation: 361605
return prices.stream()
.filter(ppr -> currency.getCode().equals(ppr.getCurrent().getCode()))
.findFirst()
.orElseThrow(NoSuchElementException::new);
Upvotes: 9