根据匹配的键值在Java中合并2个列表

时间:2019-02-19 14:18:16

标签: java list merge

我希望将列表1和列表2合并,以便获得预期的输出,如下所示。

两个列表都是Java类,并且还希望在预期结构上添加到新的Java类。

我尝试遍历List1和List2,但徒劳无功。

也尝试使用lambda表达式,但无法获得预期的输出。

将发布我尝试过的代码。

我们非常感谢您的帮助。

列表1(初级区域):

{
“One”: [
    "G13DA"
],
“Two”: [
    "G13AA",
    "G13AD"
]
}

清单2:

[
{
    "subgroupCode": "G13AA",
    "productGroup": [
        {
            "productCode": "G1A",
            "productDescription": “Two 1"
        },
        {
            "productCode": "G9B",
            "productDescription": “Two 2"
        }
    ]
},
{
    "subgroupCode": "G13AD",
    "productGroup": [
        {
            "productCode": "G4A",
            "productDescription": “Two 3”
        },
        {
            "productCode": "G9C",
            "productDescription": “Two 4”
        }
    ]
},
{
    "subgroupCode": "G13DA",
    "productGroup": [
        {
            "productCode": "G5M",
            "productDescription": “One 1"
        },
        {
            "productCode": "G9M",
            "productDescription": “One 2"
        }
    ]
}
]

预期输出:

{
   "juniorArea": [   
{      
 "juniorAreaDescription": “One”,
          "mechandisingGroup": [
         {
                "productCode": "G5M",
                "productDescription": “One 1"
         },
         {
                "productCode": "G9M",
                "productDescription": “One 2"
         }
      ]
    },
    {
      "juniorAreaDescription": “Two”,
      "mechandisingGroup": [
         {
                "productCode": "G1A",
                "productDescription": “Two 1"
         },
         {
                "productCode": "G9B",
                "productDescription": “Two 2"
         },
     {
                "productCode": "G4A",
                "productDescription": “Two 3”
         },
         {
                "productCode": "G9C",
                "productDescription": “Two 4”
         }
      ]
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

您将需要利用Google的gson库来解析您的json字符串,还需要创建其他类来保存相同的数据。将json字符串保存在文件中以读取内容并将其转换为适当的对象。

您提到的JSON字符串有错误,因为您在许多地方都使用(“)。在所有位置都将它们替换为(“)。此更改非常重要,因为如果不这样做,可能会得到Exception或不良输出。

按以下方式存储您的Json字符串:

List1.txt

{
"One": [
    "G13DA"
],
"Two": [
    "G13AA",
    "G13AD"
]
}

List2.txt

[
{
    "subgroupCode": "G13AA",
    "productGroup": [
        {
            "productCode": "G1A",
            "productDescription": "Two 1"
        },
        {
            "productCode": "G9B",
            "productDescription": "Two 2"
        }
    ]
},
{
    "subgroupCode": "G13AD",
    "productGroup": [
        {
            "productCode": "G4A",
            "productDescription": "Two 3"
        },
        {
            "productCode": "G9C",
            "productDescription": "Two 4"
        }
    ]
},
{
    "subgroupCode": "G13DA",
    "productGroup": [
        {
            "productCode": "G5M",
            "productDescription": "One 1"
        },
        {
            "productCode": "G9M",
            "productDescription": "One 2"
        }
    ]
}
]

在运行程序时,您需要更改这两个文件的路径

添加以下课程

JuniorArea.class

public class JuniorArea {
    private String juniorAreaDescription;
    private List<MerchandisingGroup> mechandisingGroup;

    public String getJuniorAreaDescription() {
        return juniorAreaDescription;
    }
    public void setJuniorAreaDescription(String juniorAreaDescription) {
        this.juniorAreaDescription = juniorAreaDescription;
    }
    public List<MerchandisingGroup> getMechandisingGroup() {
        return mechandisingGroup;
    }
    public void setMechandisingGroup(List<MerchandisingGroup> mechandisingGroup) {
        this.mechandisingGroup = mechandisingGroup;
    }

    @Override
    public String toString() {
        return "JuniorArea [juniorAreaDescription=" + juniorAreaDescription + ", mechandisingGroup=" + mechandisingGroup
                + "]";
    }
} 

JuniorAreaCollection.class

public class JuniorAreaCollection {
    private List<JuniorArea> juniorArea;

    public List<JuniorArea> getJuniorArea() {
        return juniorArea;
    }

    public void setJuniorArea(List<JuniorArea> juniorArea) {
        this.juniorArea = juniorArea;
    }

    @Override
    public String toString() {
        return "JuniorAreaCollection [juniorArea=" + juniorArea + "]";
    }
}

MerchandisingGroup.class

public class MerchandisingGroup {
    private String productCode;
    private String productDescription;

    public String getProductCode() {
        return productCode;
    }
    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }
    public String getProductDescription() {
        return productDescription;
    }
    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    @Override
    public String toString() {
        return "MechandisingGroup [productCode=" + productCode + ", productDescription=" + productDescription + "]";
    }
}

Product.class

public class Product {
    private String subgroupCode;
    private List<ProductGroup> productGroup;

    public String getSubgroupCode() {
        return subgroupCode;
    }
    public void setSubgroupCode(String subgroupCode) {
        this.subgroupCode = subgroupCode;
    }
    public List<ProductGroup> getProductGroup() {
        return productGroup;
    }
    public void setProductGroup(List<ProductGroup> productGroup) {
        this.productGroup = productGroup;
    }

    @Override
    public String toString() {
        return "Product [subgroupCode=" + subgroupCode + ", productGroup=" + productGroup + "]";
    }
}

ProductGroup.class

public class ProductGroup {
    private String productCode;
    private String productDescription;

    public String getProductCode() {
        return productCode;
    }
    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }
    public String getProductDescription() {
        return productDescription;
    }
    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    @Override
    public String toString() {
        return "ProductGroup [productCode=" + productCode + ", productDescription=" + productDescription + "]";
    }
}

最后运行下面的Main Class代码,以检查输出

public class MainRunner {
    // Use Path according to your file system
    private static String List1Path = "/home/dinesh/Desktop/Dinesh/Office/DemoProjects/List1.txt";

    // Use Path according to your file system
    private static String List2Path = "/home/dinesh/Desktop/Dinesh/Office/DemoProjects/List2.txt";

    public static void main(String...arg) throws UnsupportedEncodingException, IOException {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();

        String List1Content = new String(Files.readAllBytes(Paths.get(List1Path)), "UTF-8");

        Map<String, List<String>> map = new HashMap<>();

        map = gson.fromJson(List1Content, new TypeToken<Map<String, List<String>>>() {}.getType());

        System.out.println(map);

        String List2Content = new String(Files.readAllBytes(Paths.get(List2Path)), "UTF-8");

        List<Product> products = gson.fromJson(List2Content, new TypeToken<List<Product>>() {}.getType());

        System.out.println(products);

        List<JuniorArea> juniorAreas = new ArrayList<>();

        for (String s : map.keySet()) {
            JuniorArea juniorArea = new JuniorArea();

            juniorArea.setJuniorAreaDescription(s);

            List<MerchandisingGroup> merchandisingGroups = new ArrayList<>();

            List<String> subgroupCodes = map.get(s);

            for (String subgroupCode : subgroupCodes) {

                for (Product product : products) {

                    if (product.getSubgroupCode().equals(subgroupCode)) {
                        List<ProductGroup> productGroups = product.getProductGroup();

                        for (ProductGroup productGroup : productGroups) {
                            MerchandisingGroup merchandisingGroup = new MerchandisingGroup();

                            merchandisingGroup.setProductCode(productGroup.getProductCode());
                            merchandisingGroup.setProductDescription(productGroup.getProductDescription());

                            merchandisingGroups.add(merchandisingGroup);
                        }
                    }

                }

            }

            juniorArea.setMechandisingGroup(merchandisingGroups);

            juniorAreas.add(juniorArea);
        }

        JuniorAreaCollection collection = new JuniorAreaCollection();

        collection.setJuniorArea(juniorAreas);

        System.out.println(gson.toJson(collection));
    }
}

输出应打印在控制台上

{
  "juniorArea": [
    {
      "juniorAreaDescription": "One",
      "mechandisingGroup": [
        {
          "productCode": "G5M",
          "productDescription": "One 1"
        },
        {
          "productCode": "G9M",
          "productDescription": "One 2"
        }
      ]
    },
    {
      "juniorAreaDescription": "Two",
      "mechandisingGroup": [
        {
          "productCode": "G1A",
          "productDescription": "Two 1"
        },
        {
          "productCode": "G9B",
          "productDescription": "Two 2"
        },
        {
          "productCode": "G4A",
          "productDescription": "Two 3"
        },
        {
          "productCode": "G9C",
          "productDescription": "Two 4"
        }
      ]
    }
  ]
}