# 并行for循环

## 流式for循环

    private static void test1() {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
}
}


## 并行流式for循环

    private static void test2() {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
}
}



## forkjoin 并行流式for循环

    private static final ForkJoinPool pool = new ForkJoinPool(5);

private static void test3() {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 0; i < 100; i++) {
}
pool.submit(() -> arrayList.parallelStream().forEach(e -> {
})).join();
}


## 并行MAP

    private static void test4() {
List<String> arrayList  =new ArrayList<>();
for(int i = 0; i < 100; ++i){
}
//开始前
Map<String, String> resultMap = arrayList .parallelStream().collect(Collectors.toMap(t -> t, t -> {
try {
} catch (Exception e) {
log.error("异常：{}" + e.getMessage());
}
return "";
}));
}


## 优化并行MAP

    private static void test5() {
List<String> arrayList  =new ArrayList<>();
for(int i = 0; i < 100; ++i){
}
//优化后
Map<String, Future<String>> futureMap = arrayList.stream().collect(Collectors.toMap(t -> t, t -> threadPoolExecutor.submit(() -> t),(oldValue, newValue)->newValue));
futureMap.entrySet().stream().collect(Collectors.toMap(e->e.getKey(),e->{
try {
e.getValue().get();
}catch (Exception t) {
log.error("异常：{}" + t.getMessage());
}
return "";
}));
}


## 执行

    public static void main(String[] args) {
test1();
test2();
test3();
test4();
test5();
}


## 空处理方式

return Optional.ofNullable();

return Lists.newArrayList();

implements DependencyBase, Nullable{}

extends Nullable{}

return Collections.emptyList();

return empty();

return null;

return Optional.empty();

return new NullObject();

return new Object();

return BigDecimal.ZERO;

return Collections.emptyMap();

return Collections.emptySet();


10-22 3万+

12-12 7541

10-16 2554

12-04 2万+

08-17 9716

06-01 7928

05-09 5948

05-01 97

02-05 130

05-10 14

03-19 82万+

04-14 61万+

02-28 1万+

03-01 14万+

03-08 7万+

04-25 7万+

03-10 13万+

03-10 19万+

03-12 12万+

03-13 12万+

03-13 1万+

03-14 1万+

03-18 1万+

03-19 9万+

03-19 4万+

03-20 1万+

03-22 5万+

03-24 4万+

03-25 4万+

#### 当HR压你价，说你只值7K，你该怎么回答？

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客