Java8新特性stream和parallelStream區(qū)別
1 stream和parallelStream的區(qū)別
1.Stream 是在 Java8 新增的特性,普遍稱其為流;它不是數(shù)據(jù)結(jié)構(gòu)也不存放任何數(shù)據(jù),其主要用于集合的邏輯處理。
2.Stream流是一個(gè)集合元素的函數(shù)模型,它并不是集合,也不是數(shù)據(jù)結(jié)構(gòu),其本身并不存儲任何元素(或其地址值),它只是在原數(shù)據(jù)集上定義了一組操作。
3.Stream流不保存數(shù)據(jù),Stream操作是盡可能惰性的,即每當(dāng)訪問到流中的一個(gè)元素,才會在此元素上執(zhí)行這一系列操作。
4.Stream流不會改變原有數(shù)據(jù),想要拿到改變后的數(shù)據(jù),要用對象接收。
串行流stream:串行處理數(shù)據(jù),不產(chǎn)生異步線程。
并行流parallelStream:parallelStream提供了流的并行處理,它是Stream的另一重要特性,其底層使用Fork/Join框架實(shí)現(xiàn)。簡單理解就是多線程異步任務(wù)的一種實(shí)現(xiàn)。
建議:數(shù)據(jù)量不大的情況下建議使用stream即可,不要盲目大量使用parallelStream,因?yàn)閜arallelStream是多線程異步的,也就是說會產(chǎn)生多線程,消耗內(nèi)存不說,說不定還會更慢,并非一定會更快更好。
2 常用方法介紹
2.1 groupingBy方法
主要是轉(zhuǎn)化數(shù)據(jù)為Map,value是符合條件的集合
List<Admin> adminList = adminMapper.selectList(null); Map<Long, List<Admin>> adminMap = adminList.stream().collect(Collectors.groupingBy(Admin::getId));
2.2 toMap方法
主要是轉(zhuǎn)化數(shù)據(jù)為Map,value是該條記錄或字段值
List<Admin> adminList = adminMapper.selectList(null); Map<Long, String> adminMap = adminList.stream().collect(Collectors.toMap(Admin::getId, Admin::getRealName, (key1, key2) -> key1));
2.3 filter方法
主要是用來篩選數(shù)據(jù)的
List<Admin> adminList = adminMapper.selectList(null); adminList = adminList.stream().filter(admin -> admin.getAdminState() != null).collect(Collectors.toList());
2.4 anyMatch方法
用于判斷數(shù)據(jù),只要有一個(gè)條件滿足即返回true
List<Admin> adminList = adminMapper.selectList(null); boolean isAdmin = adminList.stream().anyMatch(admin -> admin.getAdminState() != null);
2.5 allMatch方法
用于判斷數(shù)據(jù),必須全部都滿足才會返回true
List<Admin> adminList = adminMapper.selectList(null); boolean isAdmin = adminList.stream().allMatch(admin -> admin.getAdminState() != null);
2.6 noneMatch方法
用于判斷數(shù)據(jù),全都不滿足才會返回true
List<Admin> adminList = adminMapper.selectList(null); boolean isAdmin = adminList.stream().noneMatch(admin -> admin.getAdminState() != null);
2.7 map方法
一般用于獲取屬性值
List<Admin> adminList = adminMapper.selectList(null); List<Long> adminIdList = adminList.stream().map(Admin::getId).collect(Collectors.toList());
2.8 peek方法
一般用于改變數(shù)據(jù),但是官方不建議使用
List<Admin> adminList = adminMapper.selectList(null); adminList = adminList.stream().peek(admin -> admin.setAdminState(null)).collect(Collectors.toList());
3 使用parallelStream注意事項(xiàng)
1.parallelStream是線程不安全的。
2.parallelStream適用的場景是CPU密集型的,只是做到別浪費(fèi)CPU,假如本身電腦CPU的負(fù)載很大,那還到處用并行流,那并不能起到作用。I/O密集型 磁盤I/O、網(wǎng)絡(luò)I/O都屬于I/O操作,這部分操作是較少消耗CPU資源,一般并行流中不適用于I/O密集型的操作,就比如使用并流行進(jìn)行大批量的消息推送,涉及到了大量I/O,使用并行流反而慢了很多。
3.在使用并行流的時(shí)候是無法保證元素的順序的,也就是即使你用了同步集合也只能保證元素都正確但無法保證其中的順序。
4 實(shí)戰(zhàn)比較
4.1 代碼如下
/**
* parallelStream()和stream()執(zhí)行速度測試
*/
@Test
public void test6() {
List<Integer> a = new ArrayList<>();
for (int i = 0 ; i < 1000 ; i++) {
a.add(i);
}
long b = System.currentTimeMillis();
a.parallelStream().forEach(obj->{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
log.error("出錯:", e);
}
System.out.println(obj);
});
long c = System.currentTimeMillis();
long d = System.currentTimeMillis();
a.stream().forEach(obj->{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
log.error("出錯:", e);
}
System.out.println(obj);
});
long e = System.currentTimeMillis();
System.out.println("并行耗時(shí):" + (c-b));
System.out.println("串行耗時(shí):" + (e-d));
}
4.2 執(zhí)行結(jié)果

5 結(jié)論
由上述執(zhí)行結(jié)果可知,parallelStream在同時(shí)執(zhí)行一些耗時(shí)的方法時(shí),執(zhí)行時(shí)間要優(yōu)于stream,但是使用時(shí)需要注意一些注意事項(xiàng)
到此這篇關(guān)于Java8新特性stream和parallelStream區(qū)別的文章就介紹到這了,更多相關(guān)Java8 stream parallelStream內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)評論回復(fù)功能(數(shù)據(jù)庫設(shè)計(jì))
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)評論回復(fù)功能(數(shù)據(jù)庫設(shè)計(jì)),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Spring注解@Configuration與@Bean注冊組件的使用詳解
這篇文章主要介紹了SpringBoot中的注解@Configuration與@Bean注冊組件的使用,具有很好的參考價(jià)值,希望對大家有所幫助2022-06-06
完整java開發(fā)中JDBC連接數(shù)據(jù)庫代碼和步驟
這篇文章主要介紹了完整java開發(fā)中JDBC連接數(shù)據(jù)庫代碼和步驟,需要的朋友可以參考下2015-09-09
springboot中使用ConstraintValidatorContext驗(yàn)證兩個(gè)字段內(nèi)容相同
開發(fā)修改密碼功能時(shí),通過ConstraintValidator校驗(yàn)新密碼和確認(rèn)新密碼的一致性,首先定義Matches注解和DTO對象,然后創(chuàng)建MatchesValidator類實(shí)現(xiàn)驗(yàn)證邏輯,對springboot驗(yàn)證字段內(nèi)容相同問題感興趣的朋友一起看看吧2024-10-10
java隨機(jī)生成字符串(字符隨機(jī)生成類 生成隨機(jī)字符組合)
java隨機(jī)生成字符串,字符組合多樣,可以大小字組合、大+小字符+數(shù)字等方式,大家參考使用吧2013-12-12
SpringBoot監(jiān)聽Redis key失效事件的實(shí)現(xiàn)代碼
這篇文章給大家介紹了SpringBoot實(shí)現(xiàn)監(jiān)聽Redis key失效事件的方法,文中通過代碼示例給大家講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02

