Java8 Stream 使用测试记录

上代码

package com.cash.manage.web;

import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class StreamTest {

    public static void main(String[] args){
        List<CollectionCaseTrack> collectionCaseTrackList = new ArrayList<>();
        for (int i=0; i<10000;i++
        ) {
            CollectionCaseTrack c = new CollectionCaseTrack();
            c.setOperator(Long.valueOf(i));
            collectionCaseTrackList.add(c);
        }
        long startTime = System.nanoTime();
        UniqueElementList<Long> operatorList = new UniqueElementList<>();
        collectionCaseTrackList.stream().forEach(caseTrack -> operatorList.add(caseTrack.getOperator()));
        System.out.println((System.nanoTime() - startTime) +"~~~~Stream串行总共用时" + "--" + operatorList.toList().size());
        startTime = System.nanoTime();
        UniqueElementList<Long> operatorList2 = new UniqueElementList<>();
        operatorList2.addAll(collectionCaseTrackList.stream().parallel().map(CollectionCaseTrack::getOperator).collect(Collectors.toList()));
        System.out.println((System.nanoTime() - startTime) +"~~~~Stream并行且线程安全情况下总共用时" + "--" + operatorList2.toList().size());
        startTime = System.nanoTime();
        UniqueElementList<Long> operatorList22 = new UniqueElementList<>();
        operatorList22.addAll(collectionCaseTrackList.parallelStream().map(CollectionCaseTrack::getOperator).collect(Collectors.toList()));
        System.out.println((System.nanoTime() - startTime) +"~~~~Stream并行2且线程安全情况下总共用时" + "--" + operatorList22.toList().size());
        UniqueElementList<Long> operatorList3 = new UniqueElementList<>();
        startTime = System.nanoTime();
        collectionCaseTrackList.stream().parallel().forEach(caseTrack -> operatorList3.add(caseTrack.getOperator()));
        System.out.println((System.nanoTime() - startTime) +"~~~~Stream并行且线程不安全情况下总共用时" + "--" + operatorList3.toList().size());
        UniqueElementList<Long> operatorList33 = new UniqueElementList<>();
        startTime = System.nanoTime();
        collectionCaseTrackList.parallelStream().forEach(caseTrack -> operatorList33.add(caseTrack.getOperator()));
        System.out.println((System.nanoTime() - startTime) +"~~~~Stream并行2且线程不安全情况下总共用时" + "--" + operatorList33.toList().size());
        UniqueElementList<Long> operatorList4 = new UniqueElementList<>();
        startTime = System.nanoTime();
        for (CollectionCaseTrack c:collectionCaseTrackList
        ) {
            operatorList4.add(c.getOperator());
        }
        System.out.println((System.nanoTime() - startTime) +"~~~~for循环外部迭代总共用时" + "--" + operatorList4.toList().size());
    }

    public static class UniqueElementList<E> {

        private Set<E> set = new HashSet<>();

        public void add(E element) {
            if(element != null){
                set.add(element);
            }
        }

        public void addAll(List<E> list){
            if(CollectionUtils.isNotEmpty(list)){
                set.addAll(list);
            }
        }

        public List<E> toList(){
            List<E> list = new ArrayList<>();
            list.addAll(set);
            return list;
        }
    }

    public static class CollectionCaseTrack{
        private Long id;

        private Long caseId;

        private Long operator;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public Long getCaseId() {
            return caseId;
        }

        public void setCaseId(Long caseId) {
            this.caseId = caseId;
        }

        public Long getOperator() {
            return operator;
        }

        public void setOperator(Long operator) {
            this.operator = operator;
        }
    }
}


执行代码结果

使用 parallel() 会并行执行,但是线程不安全,会造成数量减少,但是采用collect和reduce操作就是满足线程安全的了

对于简单操作,比如最简单的遍历,Stream串行API性能明显差于显示迭代

而如果是相对复杂的操作,推荐使用并行且线程安全的方式去做。

 

结论:新的知识不要急着使用,要看是否适用,选择最优方式才可以提高程序性能。

 

参考资料:

关于Java8 parallelStream并发安全的思考

Java Stream API性能测试

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值