【发布时间】:2021-10-29 06:28:17
【问题描述】:
我需要将两个列表List<Phone>和List<PhoneTwo>合并为一个组合对象Combined,在Combined中只有一个字段,即phone
[
{
"phones": [
{
"id": 1,
"phone": "44444444"
},
{
"id": 2,
"phone": "5555555"
}
],
"phonesTwo": [
{
"id": 1,
"phone": "77777777"
},
{
"id": 2,
"phone": "66666666"
}
],
"combined": null
}
]
预期结果是:
[
{
"phones": [
//data removed for brevity
],
"phonesTwo": [
//data removed for brevity
],
"combined": [
{
"phone": "44444444"
},
{
"phone": "5555555"
},
{
"phone": "77777777"
},
{
"phone": "77777777"
}
]
}
]
尝试使用flatmap,但卡在了这里,我应该如何继续?
employee.getPhones()
.stream()
.flatMap(employee.getPhonesTwo().stream()
.map(two -> {
Combined combined = new Combined();
//not sure what to do here
})).collect(Collectors.toList());
【问题讨论】:
标签: java-8 java-stream flatmap