\n\n
Now, after resolving the above issue, you should consider changing your data structure. You should create a class say Person
, to store all those attributes, and maintain a Map<Integer, Person>
, where key will be rollNo
.
What you have shown is just what you have. If you could explain some other details like, how and from where did you get those maps, and why would you possibly have the attributes of same person distributed in two different maps, may be we can help you better to formulate the data structure properly. Having a nested Map might be handled if you have small set of data, but if you have larger set of data, you will face difficult in handling them. You should certainly follow Object Oriented Approach.
\n","author":{"@type":"Person","name":"Rohit Jain"},"upvoteCount":3}}}Reputation: 589
I have two Map of LinkedHashMap in this format==> Map<String,LinkedHashMap<String,String>>
both m1 & m2 have same key values, How we combine this m1 & m2 and make m3 with all elements.
Note :Can you some one give psuedocode I will implement it.Thanks.
Input is like below format:
m1={1={rollno=1,name=chris,height=7ft},2={rollno=2,name=stephen,height=6ft}}
m2={1={rollno=1,name=chris,weight=65},2={rollno=2,name=stephen,weight=73}}
Output :
m3={1={rollno=1,name=chris,height=7ft,weight=65},2={rollno=2,name=stephen,height=6ft,weight=73}}
What I tried :
private static Map<String, LinkedHashMap<String, String>> mergeMap(Map<String,LinkedHashMap<String, String>> m1, Map<String, LinkedHashMap<String, String>> m2) {
Map<String,LinkedHashMap<String, String>> newMap = new LinkedHashMap<String, LinkedHashMap<String, String>>(m1);
for (Map.Entry<String, LinkedHashMap<String, String>> entry : m2.entrySet()) {
LinkedHashMap<String, String> t1=newMap.get(entry.getKey());
newMap.putAll(m2);
}
System.out.println("ouput :"+newMap);
return newMap;
}
Upvotes: 0
Views: 5837
Reputation: 213391
You can follow the steps below to merge the maps:
First create a newMap, passing first map - map1
as parameter. You have to use the overloaded constructor - LinkedHashMap(Map)
for that. Now you have a map with all the elements of map1
. Half of your job is done.
Map<String, Map<String, String>> newMap = new LinkedHashMap<>(map1);
Then you need to move elements from 2nd map to the newMap. For that, you would need to iterate over map2
. You can use Map#entrySet()
method to iterate over each entry in map2
. You would then use Map.Entry#getKey()
and Map.Entry#getValue()
methods to get the key and value respectively for each entry.
map2
, get the current value from newMap
, and merge the value of map2
, with the value in newMap
. Both the values are Map
. You can use Map#putAll()
method to merge the two maps. It will automatically ignore the already available keys, and add the extra key-value pair.Now, after resolving the above issue, you should consider changing your data structure. You should create a class say Person
, to store all those attributes, and maintain a Map<Integer, Person>
, where key will be rollNo
.
What you have shown is just what you have. If you could explain some other details like, how and from where did you get those maps, and why would you possibly have the attributes of same person distributed in two different maps, may be we can help you better to formulate the data structure properly. Having a nested Map might be handled if you have small set of data, but if you have larger set of data, you will face difficult in handling them. You should certainly follow Object Oriented Approach.
Upvotes: 3
Reputation: 10907
You should use java object to store complete information.
like
class Student{
int rollNo;
String name;
String height;
String weight;
}
And store your elements like
Map<Integer,Student> map = new HashMap<Integer,Student>();
it will be much easy to merge and store and manage element like this
Upvotes: 0