Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3338956
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:26:12+00:00 2026-05-18T00:26:12+00:00

I have two hashmaps. This is just example of 2 hashmaps, there can be

  • 0

I have two hashmaps. This is just example of 2 hashmaps, there can be n hashmaps.

They look like this

HashMap A = [(a, 23),(b,25),(c,43),(d,34)]
HashMap B = [(a, 32),(b,52),(d,55)]

Now I want to compare these hashmaps in such a way so that I can put the missing key of ‘c’ into HashMap B with value as 0.

How can I do that? Remember there can be n HashMaps.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:26:13+00:00Added an answer on May 18, 2026 at 12:26 am

    Let’s call the “target” HashMap the one that will get the missing keys and “sources” each of the others. For each key in each source, if the target does not contain the key then associate the zero with that key in the target:

    for (Map<String,Number> source : sources) {
      for (String key : source.keySet() ) {
        if (!target.containsKey(key)) {
          target.put(key, 0);
        }
      }
    }
    

    Now if you want to ensure that all maps have all keys from all other maps then you should first compute the entire set of keys and add the missing ones to each map:

    Set<String> allKeys = new HashSet<String>();
    for (Map<String,Number> map : allHashMaps) {
      allKeys.addAll(map.keySet());
    }
    for (Map<String,Number> map : allHashMaps) {
      for (String key : allKeys) {
        if (!map.containsKey(key)) {
          map.put(key, 0);
        }
      }
    }
    

    Both solutions perform at O(n*k) where n is the number of maps and k is the average number of keys in each map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two hashmaps and I would like to fill a third hashmap which
if i have two hashmaps, of types HashMap<Integer, HashMap<Integer, Police>> time_id_police; HashMap<Integer, HashMap<Integer, Ambulance>>
So I have a java hashmap like below: hMap.put(1, One); hMap.put(2, Two); hMap.put(3, Two);
Let’s say I have table/list like this n=3 in this case, but n can
I have two HashMaps and I can iterate both hashmaps with following code Iterator
I am getting this strange output in HashMap . I have two ArrayList<String> one
I have two values in my hashmap... How can i access those values without
So I have two questions about HashMap s in Java: What is the correct
I have built a custom hashmap using two arrays. One contains keys another values.
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.