Stream流代替For循環(huán)進(jìn)行輸出可以使代碼更簡潔。
需求:根據(jù)姓名獲取員工信息
1.建立實體類:Emp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
public class Emp { private String id; private String name; public Emp(String id, String name) { this .id=id; this .name=name; } public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } @Override public String toString() { return "Emp信息: [id=" + id + ", name=" + name + "]" ; } |
2.測試類:
(1.) 原始For寫法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
List<Emp> emps = new ArrayList<>(); emps.add( new Emp( "00101" , "張三" )); emps.add( new Emp( "00102" , "張四" )); emps.add( new Emp( "00103" , "張五" )); emps.add( new Emp( "00104" , "張六" )); emps.add( new Emp( "00105" , "張七" )); for (Emp emp : emps) { if (emp.getName().equals( "張三" )) { System.out.println(emp); return ; } } |
(2.) Stream流:
1
2
3
4
5
6
7
8
9
10
|
List<Emp> emps = new ArrayList<>(); emps.add( new Emp( "00101" , "張三" )); emps.add( new Emp( "00102" , "張四" )); emps.add( new Emp( "00103" , "張五" )); emps.add( new Emp( "00104" , "張六" )); emps.add( new Emp( "00105" , "張七" )); //filter()定義方法,toList()輸出為list List<Emp> emp=emps.stream().filter(e -> "張三" .equals(e.getName())).collect(Collectors.toList()); emp.forEach(System.out::println); |
輸出結(jié)果為:
補充知識:java中for、foreach、stream性能比較
我們在開發(fā)中循環(huán)遍歷一個數(shù)組經(jīng)常會用到,jdk8推出了一些新特性,對循環(huán)做了比較,通過代碼親測,記錄一下!
1、for循環(huán)
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(String[] args) { Long startTime = System.currentTimeMillis(); formMethod(); Long endTime = System.currentTimeMillis(); System.out.println( "result:" + (endTime - startTime)); } public static void formMethod(){ for ( int i = 0 ; i < 10000 ; i++) { System.out.println( "start::::::::::::" ); } } |
2、foreach循環(huán)(for循環(huán)的增強版)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); for ( int i = 0 ; i < 10000 ; i++) { list.add(i); } Long startTime = System.currentTimeMillis(); foreachMethod(list); Long endTime = System.currentTimeMillis(); System.out.println( "result:" + (endTime - startTime)); } /** * foreach * @param list */ public static void foreachMethod(List<Integer> list){ list.forEach(i ->{ System.out.println( "++++++++++++" ); }); } |
結(jié)論:通過代碼測試發(fā)現(xiàn)在1萬以內(nèi)的數(shù)據(jù),for循環(huán)比foreach效率要高一些;但是10萬以內(nèi)數(shù)據(jù)的時候,foreach效率更高一些!
foreach [10萬數(shù)據(jù)時間 1112 1165 1203 1115] [1萬數(shù)據(jù) 235 146 176 164 175]
for循環(huán) [10萬數(shù)據(jù)時間 1330 1437 1347] [1萬數(shù)據(jù) 110 109 141]
3、stream api
(1)、串行處理,即同步處理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); for ( int i = 0 ; i < 10000 ; i++) { list.add(i); } Long startTime = System.currentTimeMillis(); streamMethod(list); Long endTime = System.currentTimeMillis(); System.out.println( "result:" + (endTime - startTime)); } /** * stream 串行處理 * @param list */ public static void streamMethod(List<Integer> list){ list.stream().forEach(i ->{ System.out.println( "========" ); }); } |
結(jié)論:1萬以內(nèi)的數(shù)據(jù),for循環(huán)的性能要高于foreach和stream;10萬以內(nèi)的數(shù)據(jù)明顯可以看出stream效率最高,其次foreach,最后是for。
[10萬數(shù)據(jù)時間 854 892 789 844][1萬數(shù)據(jù) 172 156 219 172 171]
(2)并行處理,即stream api提供了異步處理機制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); for ( int i = 0 ; i < 10000 ; i++) { list.add(i); } Long startTime = System.currentTimeMillis(); parallelStreamMethod(list); Long endTime = System.currentTimeMillis(); System.out.println( "result:" + (endTime - startTime)); } /** * stream 并行處理 * @param list */ public static void parallelStreamMethod(List<Integer> list){ list.parallelStream().forEach(i ->{ System.out.println( "========" ); }); } |
結(jié)論:1萬以內(nèi)的數(shù)據(jù),for循環(huán)的性能要高于foreach和stream;10萬以內(nèi)的數(shù)據(jù)明顯可以看出stream效率最高,其次foreach,最后是for。
[10萬數(shù)據(jù)時間 893 844 914 972][1萬數(shù)據(jù) 219 203 234 188 ]
最終總結(jié):如果數(shù)據(jù)在1萬以內(nèi)的話,for循環(huán)效率高于foreach和stream;如果數(shù)據(jù)量在10萬的時候,stream效率最高,其次是foreach,最后是for。另外需要注意的是如果數(shù)據(jù)達(dá)到100萬的話,parallelStream異步并行處理效率最高,高于foreach和for。
以上這篇Java8之Stream流代替For循環(huán)操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_39629277/article/details/83110739