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
40 lines (36 loc) · 872 Bytes
/
Copy pathOptimizeDemo.java
File metadata and controls
40 lines (36 loc) · 872 Bytes
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
package com.learnjava.optimization;
import java.util.HashMap;
import java.util.Map;
/**
*
* 代码优化技巧总结
*
* @author lhy
* @date 2021/7/19
*/
public class OptimizeDemo {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
mergeData(map);
}
/**
* 对于通过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;
}
map.put(key, mapValue);
}
}