for (Item item : items) {
if (item pass ... some condition ...) {
found = true;
break;
}
}
for (Item item : items) {
if (item pass ... some condition ...) {
return item.getSomeValue();
}
}
forEach()
is not a cycle in its usual peremptory sense. The correct solution is to pre-filter the stream to forEach()
received only the desired values. Or one need. If it is very necessary, from stream to iterator and around it in the classic while(iterator.hasNext())
.items.stream().anyMatch(item -> /* pass some condition */);
items.stream().filter(item -> /* pass some condition */).findFirst();
// or so
items.stream().filter(item -> /* pass some condition */).findAny();
Find more questions by tags Java