In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "Java8 implementation skips this cycle and then continues to execute and jumps out of the loop and terminates the loop". In the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
Before Java8, I first used the for I loop, which was very old, and then there was the advanced for each loop, and then this jumped out of this loop and all the for loops, all simple, slightly unseen is to jump out of the multi-layer for loop.
Then there is the foreach loop out of Java8, in which break and continue don't work. Need to use return, this can only skip this loop, or will continue to execute the for loop, so how to jump out of the Java8 foreach loop?
Here is an operation for all the loops.
See how to jump out of the current loop and continue; either directly out of the for loop; or out of the multi-layer for loop.
Package com.lxk.java8.lambdaTest;import java.util.Arrays;import java.util.List;import java.util.Optional;/** * out of the for cycle * * @ author LiXuekai on 2018-10-23 * / public class BreakForeach {public static void main (String [] args) {int max = 5; List list = Arrays.asList ("123", "12345", "1234", "4321", "1234567", "5678"); continueForeachJava8 (max, list) System.out.println (); breakForeachJava8 (max, list); System.out.println (); continueFor (max, list); System.out.println (); breakFor (max, list); System.out.println (); breakManyFor (max, list);} / * * Java8 skips a foreach loop and then continues. * / private static void continueForeachJava8 (int max, List list) {list.forEach (s-> {if (s.length () > = max) {return;} System.out.println (s);}) } / * jump out of Java8's foreach cycle * not found yet, how to jump out of * / private static void breakForeachJava8 (int max, List list) {/ / wrong / / try {/ / list.forEach (s-> {/ / if (s.length ()) > = max) {/ / throw new Exception (); / /} / / System.out.println (s); / /}) / /} catch (Exception e) {/ / System.out.println (e.getMessage ()); / /} / / depending on the requirements, one of the following is recommended. Optional result = list.stream (). Filter (s-> s.length () > = max). FindFirst (); System.out.println (result.orElse (null)); boolean lxk = list.stream (). AnyMatch (s-> s.length () > = max); System.out.println (lxk);} / * * continue skip this cycle and continue. * / private static void continueFor (int max, List list) {for (String s: list) {if (s.length () > = max) {continue;} System.out.println (s);}} / * break is directly out of the for loop and no longer executes the for loop to the code. * / private static void breakFor (int max, List list) {for (String s: list) {if (s.length () > = max) {break;} System.out.println (s);}} / * break lxk directly jumps out of the multi-layer for loop and no longer executes the for loop to the code. * / private static void breakManyFor (int max, List list) {lxk: for (String S1: list) {System.out.println ("layer 1:" + S1); for (String S2: list) {System.out.println ("layer 2:" + S2); for (String S3: list) {if (s3.length () > = max) {break lxk;} System.out.println ("layer 3:" + S3) }
The following is the running result of the above code, which can be compared.
That's all for the code example of "Java8 implementation skips this loop to continue execution and jumps out of the loop and terminates the loop". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.