# Thread: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

1. Member
Join Date
Feb 2009
Posts
19
Rep Power
0

## Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

I have a small problem to solve by which I would like to merge 2 sorted maps into 1.

Map A
-------
Keys, Values
1, A
2, B
3, C
4, D
5, E

Map B
-------
Keys, Values
1, 10
2, 20
3, 30
4, 40
5, 50

Final Map should look like:
Keys, Values
A, 10
B, 20
C, 30
D, 40
E, 50

The final map would have all the values from Map A as a key and the values from Map B as values in the Final Map.

Is there a way to do this using Java?

2. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

Of course it is possible using Java; the Map itself has methods to put stuff in them, take stuff out of them and loop over both the available keys and the available values; you don't need any more to write the code to construct the third map.

The question is: can YOU do it? Perhaps you need to study the Map interface and the HashMap class a little more.

3. ## Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

I would iterate over the keys of map A, checking if the same key exists in map B, and adding the values to your final map as you go. You'll need to decide what happens if a key exists in one map but not the other.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,498
Rep Power
9

## Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

Hmm, of course you can do it. But it appears you can do this regardless of whether the initial maps are sorted or not as long as they have the same keys (baring the special situation which kjkrum mentioned). But since you need a sorted map for the final combination, you may not need to sort the first maps unless its required for some other purpose. This has nothing to do with your problem but I thought I would mention it as it seems like it would be a tad more efficient.

Regards,
Jim

5. Member
Join Date
Apr 2014
Posts
1
Rep Power
0

## Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

package org.gpk.javabrains;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class twomap {

public static void main(String[] args) {

Scanner scan =new Scanner(System.in);

System.out.println("Enter the map1 size");
int map1size = scan.nextInt();
Map map1 = new HashMap();
for(int i=0; i<map1size; i++){
System.out.println("Enter the map1 Key");
int map1key = scan.nextInt();
System.out.println("Enter the map1 Value");
String map1val = scan.next();
map1.put(map1key, map1val);
}
System.out.println("Enter the map2 size");
int map2size = scan.nextInt();
Map map2 = new HashMap();
for(int i=0; i<map2size; i++){
System.out.println("Enter the map2 Key");
int map2key = scan.nextInt();
System.out.println("Enter the map2 Value");
String map2val = scan.next();
map2.put(map2key, map2val);
}
System.out.println(map1);
System.out.println(map2);
StringBuilder s2b = new StringBuilder();
Map map3 = new HashMap();
Set set = new HashSet();
Iterator it1 = map1.keySet().iterator();
while(it1.hasNext()){

}

Iterator it2 = map2.keySet().iterator();
while(it2.hasNext()){

}
Iterator it3 = set.iterator();

while(it3.hasNext()){
Object obj = it3.next();
if(map1.containsKey(obj)&&map2.containsKey(obj)){

map3.put(map1.get(obj), map2.get(obj));
}

}
System.out.println(map3);
}

}
Last edited by karthickgpece@gmail.com; 04-04-2014 at 06:30 PM.

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,498
Rep Power
9

## Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

What's all of this??

Please read the forum rules before posting. This thread is about 9 months old so the OP probably found a solution. You should never provide a solution to a requested problem as that is spoon feeding and frowned upon (besides you solution has unnecessary code). Please put your code between code tags to properly format any examples of code (but again, no specific solutions).

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•