Java8之Stream流代替For循環(huán)操作
Stream流代替For循環(huán)進(jìn)行輸出可以使代碼更簡(jiǎn)潔。
需求:根據(jù)姓名獲取員工信息
1.建立實(shí)體類(lèi):Emp
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.測(cè)試類(lèi):
(1.) 原始For寫(xiě)法:
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流:
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é)果為:

補(bǔ)充知識(shí):java中for、foreach、stream性能比較
我們?cè)陂_(kāi)發(fā)中循環(huán)遍歷一個(gè)數(shù)組經(jīng)常會(huì)用到,jdk8推出了一些新特性,對(duì)循環(huán)做了比較,通過(guò)代碼親測(cè),記錄一下!
1、for循環(huán)
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)的增強(qiáng)版)
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é)論:通過(guò)代碼測(cè)試發(fā)現(xiàn)在1萬(wàn)以?xún)?nèi)的數(shù)據(jù),for循環(huán)比f(wàn)oreach效率要高一些;但是10萬(wàn)以?xún)?nèi)數(shù)據(jù)的時(shí)候,foreach效率更高一些!
foreach [10萬(wàn)數(shù)據(jù)時(shí)間 1112 1165 1203 1115] [1萬(wàn)數(shù)據(jù) 235 146 176 164 175]
for循環(huán) [10萬(wàn)數(shù)據(jù)時(shí)間 1330 1437 1347] [1萬(wàn)數(shù)據(jù) 110 109 141]
3、stream api
(1)、串行處理,即同步處理
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萬(wàn)以?xún)?nèi)的數(shù)據(jù),for循環(huán)的性能要高于foreach和stream;10萬(wàn)以?xún)?nèi)的數(shù)據(jù)明顯可以看出stream效率最高,其次foreach,最后是for。
[10萬(wàn)數(shù)據(jù)時(shí)間 854 892 789 844][1萬(wàn)數(shù)據(jù) 172 156 219 172 171]
(2)并行處理,即stream api提供了異步處理機(jī)制
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萬(wàn)以?xún)?nèi)的數(shù)據(jù),for循環(huán)的性能要高于foreach和stream;10萬(wàn)以?xún)?nèi)的數(shù)據(jù)明顯可以看出stream效率最高,其次foreach,最后是for。
[10萬(wàn)數(shù)據(jù)時(shí)間 893 844 914 972][1萬(wàn)數(shù)據(jù) 219 203 234 188 ]
最終總結(jié):如果數(shù)據(jù)在1萬(wàn)以?xún)?nèi)的話(huà),for循環(huán)效率高于foreach和stream;如果數(shù)據(jù)量在10萬(wàn)的時(shí)候,stream效率最高,其次是foreach,最后是for。另外需要注意的是如果數(shù)據(jù)達(dá)到100萬(wàn)的話(huà),parallelStream異步并行處理效率最高,高于foreach和for。
以上這篇Java8之Stream流代替For循環(huán)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Solr實(shí)現(xiàn)文檔檢索
Solr高度可靠、可擴(kuò)展和容錯(cuò),提供分布式索引、復(fù)制和負(fù)載平衡查詢(xún)、自動(dòng)故障轉(zhuǎn)移和恢復(fù)、集中配置等,Solr 為世界上許多最大的 Internet 站點(diǎn)的搜索和導(dǎo)航功能提供支持,本文將給大家介紹SpringBoot整合Solr實(shí)現(xiàn)文檔檢索,需要的朋友可以參考下2023-08-08
解決mybatis 中collection嵌套collection引發(fā)的bug
這篇文章主要介紹了解決mybatis 中collection嵌套collection引發(fā)的bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
SpringBoot在IDEA中實(shí)現(xiàn)熱部署的步驟
這篇文章主要介紹了SpringBoot在IDEA中實(shí)現(xiàn)熱部署的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-11-11
Java NIO.2 使用Path接口來(lái)監(jiān)聽(tīng)文件、文件夾變化
Java7對(duì)NIO進(jìn)行了大的改進(jìn),新增了許多功能,接下來(lái)通過(guò)本文給大家介紹Java NIO.2 使用Path接口來(lái)監(jiān)聽(tīng)文件、文件夾變化 ,需要的朋友可以參考下2019-05-05
原生java代碼實(shí)現(xiàn)碼云第三方驗(yàn)證登錄的示例代碼
這篇文章主要介紹了原生java代碼實(shí)現(xiàn)碼云第三方驗(yàn)證登錄的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java搭建一個(gè)springboot3.4.1項(xiàng)目?JDK21的詳細(xì)過(guò)程
這篇文章詳細(xì)介紹了如何使用IntelliJ IDEA搭建一個(gè)基于Spring Boot 3.4.1的項(xiàng)目,并使用JDK 21和Maven 3.6.3,涵蓋了環(huán)境準(zhǔn)備、項(xiàng)目創(chuàng)建、依賴(lài)管理、Maven配置、以及解決常見(jiàn)問(wèn)題的步驟,感興趣的朋友跟隨小編一起看看吧2025-01-01

