最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8新特性stream和parallelStream區(qū)別

 更新時(shí)間:2023年12月20日 09:26:49   作者:吳名氏.  
這篇文章主要介紹了Java8新特性stream和parallelStream區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1 stream和parallelStream的區(qū)別

1.Stream 是在 Java8 新增的特性,普遍稱其為流;它不是數(shù)據(jù)結(jié)構(gòu)也不存放任何數(shù)據(jù),其主要用于集合的邏輯處理。

2.Stream流是一個(gè)集合元素的函數(shù)模型,它并不是集合,也不是數(shù)據(jù)結(jié)構(gòu),其本身并不存儲(chǔ)任何元素(或其地址值),它只是在原數(shù)據(jù)集上定義了一組操作。

3.Stream流不保存數(shù)據(jù),Stream操作是盡可能惰性的,即每當(dāng)訪問(wèn)到流中的一個(gè)元素,才會(huì)在此元素上執(zhí)行這一系列操作。

4.Stream流不會(huì)改變?cè)袛?shù)據(jù),想要拿到改變后的數(shù)據(jù),要用對(duì)象接收。

串行流stream:串行處理數(shù)據(jù),不產(chǎn)生異步線程。
并行流parallelStream:parallelStream提供了流的并行處理,它是Stream的另一重要特性,其底層使用Fork/Join框架實(shí)現(xiàn)。簡(jiǎn)單理解就是多線程異步任務(wù)的一種實(shí)現(xiàn)。

建議:數(shù)據(jù)量不大的情況下建議使用stream即可,不要盲目大量使用parallelStream,因?yàn)閜arallelStream是多線程異步的,也就是說(shuō)會(huì)產(chǎn)生多線程,消耗內(nèi)存不說(shuō),說(shuō)不定還會(huì)更慢,并非一定會(huì)更快更好。

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方法

主要是用來(lái)篩選數(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ù),必須全部都滿足才會(huì)返回true

List<Admin> adminList = adminMapper.selectList(null);
boolean isAdmin = adminList.stream().allMatch(admin -> admin.getAdminState() != null);

2.6 noneMatch方法

用于判斷數(shù)據(jù),全都不滿足才會(huì)返回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適用的場(chǎng)景是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í)候是無(wú)法保證元素的順序的,也就是即使你用了同步集合也只能保證元素都正確但無(wú)法保證其中的順序。

4 實(shí)戰(zhàn)比較

4.1 代碼如下

    /**
     * parallelStream()和stream()執(zhí)行速度測(cè)試
     */
    @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("出錯(cuò):", 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("出錯(cuò):", 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

偏关县| 阜新| 宜黄县| 丹东市| 祥云县| 潜山县| 江津市| 四平市| 惠东县| 大关县| 紫金县| 西藏| 河间市| 扶绥县| 乐都县| 堆龙德庆县| 富顺县| 宝兴县| 绥德县| 彩票| 施秉县| 定西市| 保定市| 凤山市| 启东市| 大名县| 田阳县| 柳州市| 彭山县| 永寿县| 石楼县| 乃东县| 三江| 望都县| 沾化县| 静海县| 肥城市| 曲靖市| 民乐县| 阳信县| 深州市|