-1

I need to merge 2 lists List<Phone>, and List<PhoneTwo>, into one combined object Combined, in the Combined only has one field, which is phone

[
    {
        "phones": [
            {
                "id": 1,
                "phone": "44444444"
            },
            {
                "id": 2,
                "phone": "5555555"
            }
        ],
        "phonesTwo": [
            {
                "id": 1,
                "phone": "77777777"
            },
            {
                "id": 2,
                "phone": "66666666"
            }
        ],
        "combined": null
    }
]

The Expected result is:

[
    {
        "phones": [
                    //data removed for brevity
        ],
        "phonesTwo": [
                    //data removed for brevity 
        ],
        "combined": [
            {
                "phone": "44444444"
            },
            {
                "phone": "5555555"
            },
            {
                "phone": "77777777"
            },
            {
                "phone": "77777777"
            }
        ]
    }
]

Trying to use flatmap, but stuck somewhere here, how should i proceed?

employee.getPhones()
               .stream()
               .flatMap(employee.getPhonesTwo().stream()
                        .map(two -> {
                             Combined combined = new  Combined();
                              //not sure what to do here
                        })).collect(Collectors.toList());
hades
  • 4,294
  • 9
  • 46
  • 71

1 Answers1

0
class Combined {
  String phone;

  public Combined(String phone) {
    this.phone = phone;
  }
}

Just iterate both lists and add to combined array the new object with the phone

ArrayList<Combined> combined = new ArrayList<Combined>()    

for(obj in employee.getPhones()) {
  combined.add(new Combined(obj.phone))
}

for(obj in employee.getPhonesTwo()) {
  combined.add(new Combined(obj.phone))
}

System.out.println(combined)
Alexios
  • 358
  • 2
  • 8