Combine 2 object lists by index and retain key and value even if there are duplicates

Viewed 160

Have an object which has 2 lists.
Is there a way I could merge the 2 lists by index and still retain key / values if there is a conflict between the 2 lists?

This is current object with 2 lists. The lists are not always of same length.

{
    "listA": [
        {
            "text": "index 0 list 1 text",
            "icon": "index 0 list 1 icon"
        },
        {
            "text": "index 1 list 1 text",
            "icon": "index 1 list 1 icon"
        }
    ],
    "listB": [
        {
            "text": "index 0 list 2 text"
        },
        {
            "title": "index 1 list 2 title",
            "text": "index 1 list 2 text",
            "icon": "index 1 list 2 icon"
        },
        {
            "title": "index 2 list 2 title 2",
            "text": "index 2 list 2 text 2",
            "icon": "index 2 list 2 icon 2"
        },
        {
            "header": "index 3 list 2 header 2",
            "icon": "index 3 list 2 icon 2"
        }
    ]
}

After merging looking to achieve following. (A new list)

{
    "listC": [
        {
            "text": "index 0 list 1 text",
            "icon": "index 0 list 1 icon",
            "text": "index 0 list 2 text"
        },
        {
            "text":  "index  1 list 1 text",
            "icon":  "index  1 list 1 icon",
            "title": "index  1 list 2 title",
            "text":  "index  1 list 2 text",
            "icon":  "index  1 list 2 icon"
        },
        {
            "title": "index  2 list 2 title 2",
            "text":  "index  2 list 2 text  2",
            "icon":  "index  2 list 2 icon  2"
        },
        {
            "header": "index 3 list 2 header 2",
            "icon":   "index 3 list 2 icon   2"
        }
    ]
}

Understand this is not feasible due to duplicate keys. Thus one thought is to change the names for the keys in list 2 when merging.

Something like key text becomes shortCopy for list 1 and longCopy for list 2.

Thus resulting in:

{
    "shortCopy":  "index 0 list 1 text",
    "icon":  "index 0 list 1 icon",

    "longCopy": "index 0 list 2 text"
}

Is there an elegant way to achieve this via a stream?

Attempted following where I am creating a new object that holds different keys and adding that to a separate list.
Resulting in no conflicts.

Overall the question is, is there a more elegant approach to achieving this? Possibly with a Stream?
As can see, this involved multiple loops and conditionals and the need to create a a new list and object.

Looking for a cleaner solution.

My attempt to achieve this.

MyObj myObj = // some REST call;
List<NewBenefit> newBenefits = new ArrayList<>();

List<InitialBenefit> list1 = myObj.getList1();
List<InitialBenefit> list2 = myObj.getList2();

List<Benefit> newBenefits = new ArrayList<>();

// just adding the list 1 key/values to new list first. 
for (InitialBenefit item : list1) {
    Benefit benefit = new Benefit();
    benefit.setIcon(item.getIcon());
    benefit.setShortCopy(item.getText());
    newBenefits.add(benefit);
}

for (int i = 0; i < list2.size(); i++) {
    // newBenefits list now exists with some objects. 
    // so check if add new object or update keys in existing object
    // within the list
    if (newBenefits.size() > i) {
        Benefit benefit = newBenefits.get(i);
        benefit.setIcon(list2.get(i).getIcon());
        benefit.setLongCopy(list2.get(i).getText());
    } else {
        Benefit benefit = new Benefit();
        benefit.setIcon(list2.get(i).getIcon());
        benefit.setLongCopy(list2.get(i).getText());
        newBenefits.add(benefit);
    }
}
0 Answers
Related