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

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -