I have a for loop for a list that checks whether or not index values exist in the db.
Simply if any value doesn't exist, it immediately returns false.
public boolean exists(List<String> keys) {
for(String key: keys) {
boolean exists = service.existsByKey(key);
if(!exists) return false;
}
return true;
}
I tried to change it to java 8 foreach, but it doesn't work as expected.
keys.stream().forEach(k -> {
boolean exists = service.existsByKey(k);
if(!exists) return false;
});
Am I missing something? Why doesn't it go in if(!exists) staetment in forEach()?
Your return statements in forEach method are ignored.
Try to use
boolean exists = key.stream().allMatch(k -> service.existsByKey(k));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With