I have data which arrives as such (list of maps):
{
“name”: “Hello”,
"source": “One”,
"status": “Good”,
“date”: "01-05-2021 7:35:58 PM"
},
{
“name”: “Hello”,
"source": “Two”,
"status": “Good”,
“date”: "01-05-2021 7:35:58 PM"
},
{
“name”: “Goodbye”,
"source": “Three”,
"status": “Bad”,
“date”: "01-05-2021 7:35:58 PM"
},
{
“name”: “Goodbye”,
"source": “Four”,
"status": “Bad”,
“date”: "01-05-2021 7:35:58 PM"
}
So I want to group this data by “name”, but also create a new field which collects the “source” and “status” fields into a list of objects. This would mean I'd have to map the inner data to a Java class as well (call these individual objects “sourceStatus” which I've already created a class for).
{
“name”: “Hello”,
“sourceStatuses”: [
{
“source”: ”One”,
“status”: ”Good”
},
{
“source”: ”Two”,
“status”: ”Good”
}
],
“status”: “Good”,
“date”: "01-05-2021 7:35:58 PM"
},
{
“name”: “Goodbye”,
“sourceStatuses”: [
{
“source”: ”Three”,
“status”: ”Bad”
},
{
“source”: ”Four”,
“status”: ”Bad”
}
],
“status” : “Bad,
“date”: "01-05-2021 7:35:58 PM"
}
I understand the groupingBy part can be done fairly straightforwardly with Java's Collector (https://www.baeldung.com/java-groupingby-collector), but I'm not sure how to achieve the resultant set for my use case, where I not only create a new field but am also collecting then mapping inner data to a class.
Edit: "date" and "status" are going to be the same for all items with the same "name".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…