Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb0272c

Browse files
添加代码优化技巧总结
1 parent 127f829 commit fb0272c

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.learnjava.optimization;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
*
8+
* 代码优化技巧总结
9+
*
10+
* @author lhy
11+
* @date 2021年7月19日
12+
*/
13+
public class OptimizeDemo {
14+
public static void main(String[] args) {
15+
Map<String, Integer> map = new HashMap<>();
16+
mergeData(map);
17+
}
18+
19+
/**
20+
* 对于通过map来聚合数据(非Lambda方式)
21+
* @param map
22+
*/
23+
public static void mergeData(Map<String, Integer> map) {
24+
String key = "mapKey";
25+
int value = 1;
26+
// 普通方式
27+
if (map.containsKey(key)) {
28+
map.put(key, map.get(key) + value);
29+
} else {
30+
map.put(key,value);
31+
}
32+
33+
// 简洁方式
34+
Integer mapValue = map.get(key);
35+
if (null != mapValue) {
36+
mapValue += value;
37+
}
38+
map.put(key, mapValue);
39+
}
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /