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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:07:04+00:00 2026-05-28T00:07:04+00:00

I have two maps whose keys are String s and whose values are Set<MyObject>

  • 0

I have two maps whose keys are Strings and whose values are Set<MyObject>. Given two Maps, what is the easiest way to merge them such that if two keys are identical, the value is a union of the two sets. You can assume values are never null and if it is useful, we can make these Maps SortedMaps.

  • 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-28T00:07:05+00:00Added an answer on May 28, 2026 at 12:07 am

    Are we talking about HashMap instances. In that case lookup is O(1), so you can just take one map, iterate over the entries of that map, see whether the other map contains that key. If not, just add the set. If it contains the key, take the union of the two sets (by adding all elements of one set to another)

    To illustrate with some code, where I used a Set to have autocompletion in my IDE

    Map<String, Set<Double>> firstMap = new HashMap<String, Set<Double>>(  );
    Map<String, Set<Double>> secondMap = new HashMap<String, Set<Double>>(  );
    Set<Map.Entry<String, Set<Double>>> entries = firstMap.entrySet();
    for ( Map.Entry<String, Set<Double>> entry : entries ) {
      Set<Double> secondMapValue = secondMap.get( entry.getKey() );
      if ( secondMapValue == null ) {
        secondMap.put( entry.getKey(), entry.getValue() );
      }
      else {
        secondMapValue.addAll( entry.getValue() );
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
I would like to have a mapping which maps two string into one string.
How to compare two maps by their values? I have two maps containing equal
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
This is kind of last resort.. So I have two maps. typedef std::map <string,
I have two maps of type Map<Long, Integer>, one named oldValues representing the old
I have a requirement to create two different maps in C++. The Key is
I have a two ibatis sql maps that are linked together via a sub
Basically, I have to load two google maps, one dependent on the another. In
I am currently using markercluster plugin with jquery ui maps. I have two arrays

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.