forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimizeDemo.java
More file actions
70 lines (63 loc) · 1.87 KB
/
OptimizeDemo.java
File metadata and controls
70 lines (63 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.learnjava.optimization;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
*
* 代码优化技巧总结
*
* @author lhy
* @date 2021/7/19
*/
public class OptimizeDemo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
mergeData(map);
Map<String, Integer> concurrentMap = new ConcurrentHashMap<>();
concurrentMergeData(concurrentMap);
}
/**
* 对于通过map来聚合数据(非Lambda方式)
* @param map
*/
public static void mergeData(Map<String, Integer> map) {
String key = "mapKey";
int value = 1;
// 普通方式
if (map.containsKey(key)) {
map.put(key, map.get(key) + value);
} else {
map.put(key,value);
}
// 简洁方式
Integer mapValue = map.get(key);
if (null != mapValue) {
mapValue += value;
} else {
mapValue = value;
}
map.put(key, mapValue);
}
/**
* 针对mergeData里map的put操作,在并发情况下会存在put的时候,以及有其他线程已经put成功了,导致线程不安全,
* 所以需要使用并发集合列的putIfAbsent方法
* @param map
*/
public static void concurrentMergeData(Map<String, Integer> map) {
String key = "mapKey";
int value = 1;
Integer mapValue = map.get(key);
if (null != mapValue) {
mapValue += value;
} else {
mapValue = value;
}
map.putIfAbsent(key, mapValue);
// computeIfAbsent方法对map中的key只进行重新计算,如果不存在这个key,则添加到map中
map.computeIfAbsent(key, (k) -> {
// 其他计算
int a = 1, b = 2;
return a + b;
});
}
}