【问题标题】:How to split the objects based on an element from an nested object present in an arraylist in java?如何根据 java arraylist 中存在的嵌套对象中的元素拆分对象?
【发布时间】: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


    【解决方案1】:

    您可以使用 JSON 库,例如约森做改造。

    https://github.com/octomix/josson

    反序列化

    Josson josson = Josson.fromJsonString(
        "[" +
        "    {" +
        "        "_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"" +
        "            }" +
        "        ]" +
        "    }," +
        "    {" +
        "        "_id": "111111111111111111111111"," +
        "        "name": "B"," +
        "        "users": [" +
        "            {" +
        "                "id": "22222222222222222222222"," +
        "                "owner": "John XX"," +
        "                "purchase_date": "2022-12-25"," +
        "                "status": "active"," +
        "                "parent_user_id": "22222222222222222222222"" +
        "            }" +
        "        ]" +
        "    }" +
        "]");
        
    

    转型

    JsonNode node = josson.getNode(
        "[]@" + // 1
        ".users" + // 2
        ".group(parent_user_id)" + // 3
        ".map(parent_user_id:, ..._id, ...name, ...description, users:elements)" + // 4
        ".@flatten()"); // 5
    System.out.println(node.toPrettyString());
    
    1. 将每个元素转移到单独的分支
    2. 前往节点users
    3. 分组parent_user_id
    4. 删除parent_user_id,退后两步获取_id/name/description,并将elements重命名为users
    5. 将所有分支结果合并到一个数组中,然后展平数组

      输出

      [ {
        "_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"
        } ]
      }, {
        "_id" : "111111111111111111111111",
        "name" : "B",
        "users" : [ {
          "id" : "22222222222222222222222",
          "owner" : "John XX",
          "purchase_date" : "2022-12-25",
          "status" : "active",
          "parent_user_id" : "22222222222222222222222"
        } ]
      } ]
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-04
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多