简体   繁体   English

难以将 Google Guava map 元素添加到 java 中的数组

[英]Difficulty adding Google Guava map elements to array in java

Hello i need to add several elements from the Google Multimap class to Arrays in order to be able to pass them to the JAXB marshall method for marshalling.您好,我需要将 Google Multimap class 中的几个元素添加到 Arrays 以便能够将它们传递给 JAXB 编组方法进行编组。

public void marshall(Multimap<String, Product> supplierProducts) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Product.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        Product[] panasonic = new Product[10];
        Product[] apple = new Product[10];
        Product[] sony = new Product[10];

        for (Product product : supplierProducts.values()) {
        if (product.getSupplier().equals("Panasonic")) {

        } else if (product.getSupplier().equals("Apple")) {
            apple
        } else {
            sony
        }
        }
        marshaller.marshal(panasonic, new File("src/output/panasonic.xml"));
        marshaller.marshal(apple, new File("src/output/apple.xml"));
        marshaller.marshal(sony, new File("src/output/sony.xml"));

    }

In the map i have Product objects.The key is one of the 3 values:panasonic,sony or apple while the values is a product with several fields.I need to add to each array the products that contain only the specific key so that i can write to 3 sepparate xml files the products from each supplier.在 map 中,我有 Product 对象。键是 3 个值之一:松下、索尼或苹果,而值是具有多个字段的产品。我需要将仅包含特定键的产品添加到每个数组中,以便我可以将每个供应商的产品写入 3 个单独的 xml 文件。 So the problem is that i do not know how to do this.It got really confusing really fast.Any help would be greatly appreciated !所以问题是我不知道该怎么做。它很快就变得非常混乱。任何帮助将不胜感激!

you can use below approach to solve problem, Version-2:您可以使用以下方法来解决问题,版本 2:

public void marshallV1(Multimap<String, Product> supplierProducts) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Product.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        List<Product> panasonic = new ArrayList<>();
        List<Product> apple = new ArrayList<>();
        List<Product> sony = new ArrayList<>();

        for (Product product : supplierProducts.values()) {
        if (product.getSupplier().equals("Panasonic")) {
           panasonic.add(product ); 
        } else if (product.getSupplier().equals("Apple")) {
            apple.add(product ); 
        } else {
            sony.add(product ); 
        }
        }
        marshaller.marshal(panasonic, new File("src/output/panasonic.xml"));
        marshaller.marshal(apple, new File("src/output/apple.xml"));
        marshaller.marshal(sony, new File("src/output/sony.xml"));

    } 

Version-2:版本 2:

public void marshallV2(Multimap<String, List<Product>> supplierProducts) throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(Product.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            List<Product> panasonic = supplierProducts.get("panasonic ");
            List<Product> apple = supplierProducts.get("apple ");
            List<Product> sony = supplierProducts.get("sony ");
    
  
            marshaller.marshal(panasonic, new File("src/output/panasonic.xml"));
            marshaller.marshal(apple, new File("src/output/apple.xml"));
            marshaller.marshal(sony, new File("src/output/sony.xml"));
    
        } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM