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

Appearance settings

Commit fb0272c

Browse filesBrowse files
committed
添加代码优化技巧总结
1 parent 127f829 commit fb0272c
Copy full SHA for fb0272c

File tree

Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed
Open diff view settings
Collapse file
+40Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.