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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:07:35+00:00 2026-06-16T02:07:35+00:00

I have two maps that have a String as the key, and Set as

  • 0

I have two maps that have a String as the key, and Set as its value. These two maps can share the same keys. I’m trying to merge the two Set values together if the two maps have the same key. The problem is, the second map might be null, and since not all keys are shared between the two maps, the Sets might also be null. I’ve come up with a couple options, but they all look pretty messy. Was wondering if anyone had a more efficient/prettier way of doing it. This is what I have so far:

Set<String> mergedSet = (firstMap.containsKey(commonKey)) ? firstMap.get(commonKey) : new HashSet<String>();

mergedSet.addAll(secondMap != null && secondMap.containsKey(commonKey) ? secondMap.get(commonKey) : new HashSet<String>());

  • 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-16T02:07:36+00:00Added an answer on June 16, 2026 at 2:07 am

    I would use Guava’s HashMultimap instead of a Map<String, Set<String>>. It has the following advantages:

    • shortcut methods to add multiple values for a given key, without caring if a Set already exists for this key of not
    • always returns a non-null Set when get(key) is called, even if nothing was ever stored for this key.

    So your code would become:

    Set<String> mergedSet = Sets.union(firstMultimap.get(commonKey),
                                       secondMultimap.get(commonKey));
    

    The set would simply be a view over the two sets, which avoids copying every element. But if you want a copy, then do

    Set<String> mergedSet = Sets.newHashSet(Sets.union(firstMultimap.get(commonKey),
                                                       secondMultimap.get(commonKey)));
    

    If you don’t want to go with an external library, then your code is pretty much OK. I would use Collections.singletonSet() for the second fallback set though, to avoid an unnecessary empty set creation. And be careful: your code modified the first set. It doesn’t make a copy of it. So in the end, every set of the first map is in fact a merged set.

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

Sidebar

Related Questions

I have two maps whose keys are String s and whose values are Set<MyObject>
I have two Maps that contain the same type of Objects: Map<String, TaskJSO> a
If I have two maps which are guaranteed to have exactly the same set
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
I have a requirement to create two different maps in C++. The Key is
I would like to have a mapping which maps two string into one string.
I have two maps, tk1 and tk2 with the following structure: std::map<std::string, double>tk1; std::map<std::string,
I have two tables which share common fields. Rather than re-map all of these
I have rails application that has two view ports that uses the same data.
We have code with two complementary Maps like this: private final Map<Integer, String> idToName

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.