上代码
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性能明显差于显示迭代
而如果是相对复杂的操作,推荐使用并行且线程安全的方式去做。
结论:新的知识不要急着使用,要看是否适用,选择最优方式才可以提高程序性能。
参考资料: