0

I have 2 arraylists(of same size) in json as below:

List<HashMap<String, String>> list1 = new Arraylist<HashMap<String, String>>();

List<HashMap<String, String>> list2 = new Arraylist<HashMap<String, String>>();

I want to merge the above lists into a single one.

For ex:

List1:

[{ " key1": "value1", " key2": "value2"}, 
{ " key11": "value11", " key22": "value22"}]

List2:

[{ " key3": "value3"},{" key33": "value33"}]

Expected output:

[{ " key1": "value1", " key2": "value2", " key3": "value3"}, 
{ " key11": "value11", " key22": "value22", " key3": "value3"}]

An early response will be highly appreciated. Thanks a lot in advance.

3
  • What is your original input? Are these the JSON values or the Java POJOs? Commented Apr 8, 2016 at 6:12
  • Original input are the lists that I want to merge and are json values Commented Apr 8, 2016 at 6:15
  • Then have a look here Commented Apr 8, 2016 at 6:24

4 Answers 4

2

If the two ArrayList have the same type (they seem to be arraylist of maps):

 list1.addAll(list2)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. list1.addAll(list2) gives the following O/p: [{{ " key1": "value1", " key2": "value2"},{ " key3": "value3"}}, {{ " key11": "value11", " key22": "value22"},{ " key3": "value3"}}]
1

Your expected output probably be:

[{ " key1": "value1", " key2": "value2", " key3": "value3"}, { " key11": "value11", " key22": "value22", " key33": "value33"}]

You didn't indicate whether two lists had same size or not. According to your example, I assumed the lists had same size. So the code could be like below:

for (int i = 0; i < list1.size(); i++) {
  list1.get(i).putAll(list2.get(i));
}

1 Comment

Superb!! Thanks a tonne. This is what i was looking for. I will edit the question to mention about the same size.
0

You Just have to retrieve the Hashmap from the individual Lists and then merge them all together

for eg

HashMap m = new HashMap();
        m.put("key1", "val1");
        m.put("key2", "val2");

        HashMap m1 = new HashMap();
        m1.put("key3", "val3");
HashMap mergeMap = new HashMap();
        mergeMap.putAll(m);
        mergeMap.putAll(m1);

        System.out.println("Map:" + mergeMap);

Output

Map:{key3=val3, key2=val2, key1=val1}

Comments

0

Edit: you can do like this

    for (HashMap<String, String> m : list2) {
        HashMap<String, String> t = list1.get(list2.indexOf(m));
        for(String key: t.keySet()){
            m.put(key, t.get(key));
        }
    }

Now Merged items are in list2.

5 Comments

Thanks for your response. list1.addAll(list2) doesnt give the expected O/p i.e. it gives: [{{ " key1": "value1", " key2": "value2"},{ " key3": "value3"}}, {{ " key11": "value11", " key22": "value22"},{ " key3": "value3"}}]
I have checked, the correct json is : [{" key1":"value1"," key2":"value2"},{" key3":"value3"},{" key11":"value11"," key22":"value22"},{" key3":"value3"}] your json is invalid in 1st place [{{ " key1": "value1", " key2": "value2"},{ " key3": "value3"}}, {{ " key11": "value11", " key22": "value22"},{ " key3": "value3"}}] how you have got this wrong one ?
Sorry if I was not clear. As you have noted, after doing list1.addAll(list2)... O/P is [{" key1":"value1"," key2":"value2"},{" key3":"value3"},{" key11":"value11"," key22":"value22"},{" key3":"value3"}]. However as stated in the question, the O/P I need is: [{ " key1": "value1", " key2": "value2", " key3": "value3"}, { " key11": "value11", " key22": "value22", " key3": "value3"}]
Ahh, I got it you want to merge objects rather lists. I guess there is no built in method for this you have to write it on your own.
See my Edit on answer i wrote code for you :) which generate json of yours [{" key1":"value1"," key2":"value2"," key3":"value3"},{" key11":"value11"," key22":"value22"," key3":"value3"}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.