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 daa79ea

Browse filesBrowse files
committed
添加lambda mapMerge例子
1 parent fb0272c commit daa79ea
Copy full SHA for daa79ea

File tree

Expand file treeCollapse file tree

2 files changed

+59
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+59
-1
lines changed
Open diff view settings
Collapse file

‎JdkLearn/src/main/java/com/learnjava/lambda/LambdaMapDemo.java‎

Copy file name to clipboardExpand all lines: JdkLearn/src/main/java/com/learnjava/lambda/LambdaMapDemo.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void test01() {
4141
}
4242

4343
/**
44-
* HashMap的merge方法,如果key相同,则通过merge来对key相同的袁术进行处理
44+
* HashMap的merge方法,如果key相同,则通过merge来对key相同的元素进行处理
4545
*/
4646
public static void test02() {
4747
String key = "money";
Collapse file
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.learnjava.lambda;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 关于Map的合并操作
8+
*
9+
* @author lhy
10+
* @date 2021/7/20
11+
*/
12+
public class LambdaMapMerge {
13+
14+
public static void main(String[] args) {
15+
// mapMerge();
16+
// mapMerge2();
17+
}
18+
19+
/**
20+
* value为int类型的map merge操作,将两个map,相同key merge在一起
21+
*
22+
* key:string
23+
* value:int
24+
*/
25+
public static void mapMerge() {
26+
Map<String, Integer> map1= new HashMap<>();
27+
map1.put("one",1);
28+
map1.put("two",2);
29+
map1.put("three",3);
30+
Map<String,Integer> map2= new HashMap<>();
31+
map2.put("one",1);
32+
map2.put("two",2);
33+
34+
map1.forEach((key, value) -> map2.merge(key, value, Integer::sum));
35+
System.out.println(map2);
36+
}
37+
38+
/**
39+
* value为int类型的map merge操作,将两个map,相同key merge在一起
40+
*
41+
* key:string
42+
* value:String
43+
*/
44+
public static void mapMerge2() {
45+
Map<String,String> map1= new HashMap<>();
46+
map1.put("one","1");
47+
map1.put("two","2");
48+
map1.put("three","3");
49+
Map<String,String> map2= new HashMap<>();
50+
map2.put("one","1");
51+
map2.put("two","2");
52+
53+
map1.forEach((key, value) -> map2.merge(key, value,(total, num) -> String.valueOf(Integer.parseInt(total) + Integer.parseInt(num))));
54+
55+
System.out.println(map2);
56+
57+
}
58+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.