In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what is the difference between java's for (), enhanced for (), foreach (), stream (). Foreach ()", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between java's for (), enhanced for (), foreach (), stream (). Foreach ()"!
Test one
Public class TestFor {private static void doSome (String s) {} public static void main (String [] args) {/ * add data * / List list = new ArrayList (); for (int I = 0; I
< 10; i++) { list.add("第" + i + "条数据"); } /*for循环*/ long st1 = System.currentTimeMillis(); for (int i = 0; i < list.size(); i++) { doSome(list.get(i)); } /*增强for循环*/ long st2 = System.currentTimeMillis(); for (String s : list) { doSome(s); } /*foreach*/ long st3 = System.currentTimeMillis(); list.forEach(s ->DoSome (s); / * stream.foreach*/ long st4 = System.currentTimeMillis (); list.stream (). ForEach (s-> doSome (s)); long st5 = System.currentTimeMillis (); print (st1, st2, st3, st4, st5) } / * time output * / public static void print (long S1, long S2, long S3, long S4, long S5) {System.out.println ("for cycle:" + (S2-S1) + "ms"); System.out.println ("enhanced for cycle:" + (S2-S2) + "ms") System.out.println ("foreach loop:" + (S4-S3) + "ms"); System.out.println ("stream.foreach loop:" + (S5-S4) + "ms");}}
Results:
For loop: 0ms enhanced for loop: 0ms foreach loop: 129ms stream.foreach loop: 3ms
Test two
List list = new ArrayList (); for (int I = 0; I < 1000; iTunes +) {list.add ("item" + I + "data");}
Results:
For loop: 1ms enhanced for loop: 1ms foreach loop: 121ms stream.foreach loop: 3ms
Test three
List list = new ArrayList (); for (int I = 0; I < 1000000; iTunes +) {list.add ("item" + I + "data");}
Results:
For loop: 17ms enhanced for loop: 23ms foreach loop: 165ms stream.foreach loop: 18ms
Test four
Private static void doSome (String s) {try {Thread.sleep (1);} catch (InterruptedException e) {e.printStackTrace ();}}
Results:
For loop: 184ms enhanced for loop: 169ms foreach loop: 224ms stream.foreach loop: 116ms so far, I believe you have a deeper understanding of what is the difference between java's for (), enhanced for (), foreach (), stream (). Foreach (), you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.