<!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG:--><!--DEBUG:dc3-united-states-software-in-english-pdf-2--><!--DEBUG-spv-->{"id":1849442,"date":"2021-02-27T18:51:00","date_gmt":"2021-02-27T16:51:00","guid":{"rendered":"http:\/\/nhub.news\/?p=1849442"},"modified":"2021-02-28T00:05:08","modified_gmt":"2021-02-27T22:05:08","slug":"java-joy-merge-maps-using-stream-api","status":"publish","type":"post","link":"http:\/\/nhub.news\/de\/2021\/02\/java-joy-merge-maps-using-stream-api\/","title":{"rendered":"Java Joy: Merge Maps Using Stream API"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>Learn how to use the Java Stream API to merge multiple Map instances into a new Map using a remapping function for duplicate keys.<\/b><br \/>\nJoin the DZone community and get the full member experience. In Java, we can merge a key\/value pair into a Map with the merge method. The first parameter is the key, the second the value, and the third parameter of the merge method is a remapping function that is applied when the key is already present in the Map instance. The remapping function has the value of the key in the original Map and the new value. We can define in the function what the resulting value should be. If we return null, the key is ignored. If we want to merge multiple Map instances, we can use the Stream API. We want to convert the Map instances to a stream of Map. Entry instances, which we then turn into a new Map instance with the toMap method from the class Collectors. The toMap method also takes a remapping function when there is a duplicate key. The function defines what the new value is based on the two values of the duplicate key that was encountered. We can choose to simply ignore one of the values and return the other value. But we can also do some computations in this function, for example creating a new value using both values. In the following example, we use the Stream API to merge multiple Map instances into a new Map using a remapping function for duplicate keys: Written with Java 15. Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here. Opinions expressed by DZone contributors are their own.<\/p>\n<script>jQuery(function(){jQuery(\".vc_icon_element-icon\").css(\"top\", \"0px\");});<\/script><script>jQuery(function(){jQuery(\"#td_post_ranks\").css(\"height\", \"10px\");});<\/script><script>jQuery(function(){jQuery(\".td-post-content\").find(\"p\").find(\"img\").hide();});<\/script>","protected":false},"excerpt":{"rendered":"<p>Learn how to use the Java Stream API to merge multiple Map instances into a new Map using a remapping function for duplicate keys. Join the DZone community and get the full member experience. In Java, we can merge a key\/value pair into a Map with the merge method. The first parameter is the key, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1849441,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1849442"}],"collection":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/comments?post=1849442"}],"version-history":[{"count":1,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1849442\/revisions"}],"predecessor-version":[{"id":1849443,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/1849442\/revisions\/1849443"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media\/1849441"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media?parent=1849442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/categories?post=1849442"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/tags?post=1849442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}