java - How do you stop search in a loop once you meet the condition -


how stop each loop proceeding when condition false? because can let x = newx when object in collection true condition. thank help. break won't help, tried.

for (char  h : collection){     if ( condition == false )     {      }     else{         x = newx;     } } 

simplest way use break statement. want set new x value when elements of list passing condition.

boolean allmatch = true; (char  h : collection) {     if (!condition) {         allmatch = false;         break;     } } if (allmatch) {     x = newx; } 

if want check if element sof loop matching condition can use java 8 feature:

boolean allmatch = list.stream().allmatch(element -> condition); if (allmatch) {     x = newx; } 

Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -