I have 2 array lists as follows.
One array contains student information mapped with id, name and city. The other array contains the marks details mapped with id, marksandgrade. The goal is to compare each of these elements against each other and combine them based on the common field id in both the ArrayList`.
student_details=[
{
"id": 1,
"name": "sam",
"city": "Chennai"
},
{
"id": 2,
"name": "peter",
"city": "Chennai"
}
]
student_grades=[
{
"id": 1,
"marks": 95,
"grade": "A"
},
{
"id": 2,
"marks":63,
"grade": "B"
}
]
The result should look as follows.
[
{
"id": 1,
"name": "sam",
"city": "Chennai",
"marks": 95,
"grade": "A"
},
{
"id": 2,
"name": "peter",
"city": "Chennai",
"marks":63,
"grade": "B"
}
]
I used the below code to achieve the same in javascript. How do I implement this in java?
async mergeData(data1, data2, key) {
const result = data1.map(a =>
Object.assign({}, a,
data2.find(b => b[key] === a[key])
)
)
return result
}