Created
June 28, 2019 19:37
-
-
Save akshanshjain95/e685072ca624e1c99c41aaec383a497f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class MergeTwoMaps { | |
public static void main(String[] args) { | |
Map<String, Long> map = new HashMap<>(); | |
map.put("Apple", 2L); | |
map.put("Banana", 2L); | |
Map<String, Long> map1 = new HashMap<>(); | |
map1.put("Apple", 2L); | |
map1.put("Orange", 2L); | |
Map<String, Long> resultMap = Stream.concat(map.entrySet().stream(), map1.entrySet().stream()) | |
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, | |
(countInFirstMap, countInSecondMap) -> countInFirstMap + countInSecondMap)); | |
System.out.println(resultMap); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment