Thursday, October 31, 2019

How To Combine 2 Map Inward Java? Example Tutorial

You tin combine 2 maps inward Java past times using the putAll() method of java.util.Map interface. This method copies all the mappings from 1 Map to another, like, if you lot telephone phone it similar first.putAll(second), thence all mappings from the mo Map volition last copied into the showtime Map. Which way if the showtime map contains five elements together with the mo map contains 10 elements or mapping, thence the combined map volition incorporate fifteen or fewer mappings. In the instance of duplicate keys, the value is overridden or updated from the mo map. For example, if the showtime map has a mapping 1 -> One together with mo map has mapping 1 -> ONE thence later combining the mo map into the showtime one, the value of 1 volition last ONE, i.e. the value from the mo map.

The putAll() method copies all of the mappings from the specified map to this map. The trial of this telephone phone is similar to iterating over the map together with calling put(k, v) on this Map 1 time for each mapping from telephone commutation k to value v inward the specified map.

Btw, this is an optional functioning for whatsoever Map implementation, but HashMap implements this method. You volition acquire the UnsupportedOperationException if the putAll functioning is non supported past times this map.

The putAll() method besides throws NullPointerException if the specified map is null, or if this map does non permit nix keys or values, together with the specified map contains nix keys or values.

It tin besides throw ClassCastException if the flat of a telephone commutation or value inward the specified map prevents it from beingness stored inward this map. Also, the direct of this functioning is undefined if the specified map is modified piece the functioning is inward progress.

Now that nosotros know plenty close how to combine 2 Map inward Java or re-create values from 1 Map to approximately other let's encounter a working instance to fully sympathize the concept together with larn how to purpose its code. Btw, if you lot are merely starting amongst Java together with necessitate to a greater extent than information on Map class, thence you lot should besides see The Complete Java Masterclass course on Udemy. One of the most up-to-date course of report to larn Java




Java Program to Copy Values from One Map to Other

Here is a elementary programme to re-create values from 1 map to other inward Java. This way, you lot tin combine mappings, similar both keys together with values. For example, if you lot have 2 maps of orders from dissimilar systems, you lot tin combine them to calculate the amount notional value.

In our case, nosotros merely accept 2 maps where numbers are mapped to their String values. In 1 map nosotros accept mapped discover to equivalent string written inward a small-scale instance together with other maps nosotros accept done the same thing, but strings are inward upper-case alphabetic lineament order.

This has been done to present that inward the instance of duplicate keys, the values are updated from the mo map. This is the basic belongings of Map (see tutorial)
  • My favorite gratis courses to larn Data Structure together with Algorithms (courses)
  • The divergence betwixt Hashtable together with HashMap inward Java? (answer)
  • The divergence betwixt HashMap together with ConcurrentHashMap inward Java? (answer)
  • What is the best way to iterate over a Map? (tutorial)
  • How produce you lot notice if a Map contains a key? (tutorial)
  • 5 Courses to larn Java Programming for beginners (courses)
  • What is the divergence betwixt HashMap, TreeMap, together with LinkedHashMap inward Java? (answer)
  • How to take a key-value couple from HashMap inward Java? (tutorial)
  • My favorite gratis courses to larn Java in-depth (courses)
  • How to form a HashMap past times keys inward Java 8? (tutorial)
  • How to form a Map past times keys together with values inward Java? (tutorial)
  • The divergence betwixt HashMap together with LinkedHashMap inward Java? (answer)

  • If you lot similar this article, thence delight portion it amongst your friends together with colleagues. If you lot accept whatsoever enquiry or doubtfulness then, delight drib a comment.

    No comments:

    Post a Comment