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 8257763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:18:24+00:00 2026-06-08T02:18:24+00:00

I have two maps with key as an integer and value as a double.

  • 0

I have two maps with key as an integer and value as a double.

I want to create a third map sorted on key and value would be a list of doubles from both the maps.

map1:

1, 90.00
5, 75.45
8, 76.50

map2:

4, 12.00
5, 322.09
8, 11.09
9, 21.00

final map:

1, (90.00,0.00)
5, (75.45,322.09)
8, (76.50,11.09)
9, (0.00, 21.00)

As clear from above, if a key in one of the map is missing in the other map, the value in the final map for the other map should be defaulted to 0.00

Map firstMap = new HashMap<Integer, Double>();
Map secondMap = new HashMap<Integer, Double>();

Map finalMap = new HashMap<Integer, List<Double>>();

firstMap.put(1, 90.00);
firstMap.put(5, 75.45);
firstMap.put(8, 76.50);

secondMap.put(4, 12.00);
secondMap.put(5, 322.09);
secondMap.put(8, 11.09);
secondMap.put(9, 21.00);

I can use putAll method to put all keys into the third map. But how to put the values as I want ?

Thanks for reading!

  • 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-06-08T02:18:25+00:00Added an answer on June 8, 2026 at 2:18 am

    Simply do

    // Create a sorted map
    Map<Integer, List<Double>> finalMap = new TreeMap<Integer, List<Double>>();
    
    Set<Integer> keys = new HashSet<Integer>();
    keys.addAll(firstMap.keySet());
    keys.addAll(secondMap.keySet());
    for (Integer key : keys) {
        double first  = firstMap.containsKey(key)  ? firstMap.get(key)  : 0.0;
        double second = secondMap.containsKey(key) ? secondMap.get(key) : 0.0;
        finalMap.put(key, Arrays.asList(first, second));
    }
    

    Using Guava it’s as simple as

    Multimap<Integer, Double> finalMultiMap = ArrayListMultimap.create();
    finalMultiMap.putAll(Multimaps.forMap(firstMap));
    finalMultiMap.putAll(Multimaps.forMap(secondMap));
    
    Map<Integer, Collection<Double>> finalMap = finalMultiMap.asMap();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to create two different maps in C++. The Key is
I have two maps of type Map<Long, Integer>, one named oldValues representing the old
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
I have the following two boost::fusion maps. namespace bf = boost::fusion; typedef bf::map<bf::pair<char,float>,bf::pair<int,double>> FusionMap;
I have two maps, tk1 and tk2 with the following structure: std::map<std::string, double>tk1; std::map<std::string,
I would like to have a mapping which maps two string into one string.
In my current C++-project I have an STL map which maps integer keys onto
I have two maps on a page, one is a map of the world,
In java, I want to compare two maps, like below, do we have existing
This is kind of last resort.. So I have two maps. typedef std::map <string,

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.