【发布时间】:2023-01-16 02:18:47
【问题描述】:
我有以下对象的列表(例如,仅显示数组中的一个对象)。如何在 JAVA 中将用户列表拆分为具有相同原始对象的不同对象。我已经给出了从 db 获得的结果,以及在 java 中处理后的预期结果。
正如您在附加的 JSON(第二个 JSON)中看到的结果所期望的那样,除了每个对象中的用户数组列表之外,所有对象都保持不变。我们必须在 users 数组中的所有对象中根据 "parent_user_id" 拆分数组,因此具有相同 "parent_user_id" 的公共对象将作为一个整体在一个对象中。
结果来自数据库:
[
{
"_id" : "63a8808652f40e1d48a3d1d7",
"name" : "A",
"description" : null,
"users" : [
{
"id" : "63a8808c52f40e1d48a3d1da",
"owner" : "John Doe",
"purchase_date" : "2022-12-25",
"status" : "active",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
},
{
"id" : "63a880a552f40e1d48a3d1dc",
"owner" : "John Doe 1",
"purchase_date" : "2022-12-25",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
},
{
"id" : "63a880f752f40e1d48assddd"
"owner" : "John Doe 2",
"purchase_date" : "2022-12-25",
"parent_user_id" : "63a8808c52f40e1d48a3d1da"
},
{
"id" : "63a880f752f40e1d48a3d207"
"owner" : "John Doe 11",
"dt" : "2022-12-25",
"status" : "inactive",
"parent_user_id" : "63a880f752f40e1d48a3d207"
},
{
"id" : "63a880f752f40e1d48agfmmb"
"owner" : "John Doe 112",
"dt" : "2022-12-25",
"status" : "active",
"parent_user_id" : "63a880f752f40e1d48agfmmb"
}
{
"id" : "63a880f752f40e1d48agggg"
"owner" : "John SS",
"dt" : "2022-12-25",
"status" : "inactive",
"parent_user_id" : "63a880f752f40e1d48agggg"
}
{
"id" : "63a880f752f40e1d487777"
"owner" : "John SS",
"dt" : "2022-12-25",
"parent_user_id" : "63a880f752f40e1d48agggg"
}
]
}
]
我在java中需要的结果
[
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a8808c52f40e1d48a3d1da",
"owner": "John Doe",
"purchase_date": "2022-12-25",
"status": "active",
"parent_user_id": "63a8808c52f40e1d48a3d1da"
},
{
"id": "63a880a552f40e1d48a3d1dc",
"owner": "John Doe 1",
"purchase_date": "2022-12-25",
"parent_user_id": "63a8808c52f40e1d48a3d1da"
},
{
"id": "63a880f752f40e1d48assddd",
"owner": "John Doe 2",
"purchase_date": "2022-12-25",
"parent_user_id": "63a8808c52f40e1d48a3d1da"
}
]
},
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a880f752f40e1d48a3d207",
"owner": "John Doe 11",
"dt": "2022-12-25",
"status": "inactive",
"parent_user_id": "63a880f752f40e1d48a3d207"
}
]
},
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a880f752f40e1d48agfmmb",
"owner": "John Doe 112",
"dt": "2022-12-25",
"status": "active",
"parent_user_id": "63a880f752f40e1d48agfmmb"
}
]
},
{
"_id": "63a8808652f40e1d48a3d1d7",
"name": "A",
"description": null,
"users": [
{
"id": "63a880f752f40e1d48agggg",
"owner": "John SS",
"dt": "2022-12-25",
"status": "inactive",
"parent_user_id": "63a880f752f40e1d48agggg"
},
{
"id": "63a880f752f40e1d487777",
"owner": "John SS",
"dt": "2022-12-25",
"parent_user_id": "63a880f752f40e1d48agggg"
}
]
}
]
【问题讨论】:
标签: java arrays collections stream