难以将 Google Guava 映射元素添加到 java 中的数组
Difficulty adding Google Guava map elements to array in java
您好,我需要将 Google Multimap class 中的几个元素添加到数组,以便能够将它们传递给 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"));
}
在地图中我有 Product objects.The key 是 3 values:panasonic,sony 或 apple 之一,而 values 是一个产品,有几个 fields.I 需要添加到每个数组仅包含特定密钥的产品,以便我可以将每个供应商的产品写入 3 个单独的 xml 文件。
所以问题是我不知道该怎么做 this.It 真的很困惑 fast.Any 非常感谢帮助!
您可以使用以下方法解决问题,
版本 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"));
}
版本 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"));
}
您好,我需要将 Google Multimap class 中的几个元素添加到数组,以便能够将它们传递给 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"));
}
在地图中我有 Product objects.The key 是 3 values:panasonic,sony 或 apple 之一,而 values 是一个产品,有几个 fields.I 需要添加到每个数组仅包含特定密钥的产品,以便我可以将每个供应商的产品写入 3 个单独的 xml 文件。 所以问题是我不知道该怎么做 this.It 真的很困惑 fast.Any 非常感谢帮助!
您可以使用以下方法解决问题, 版本 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"));
}
版本 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"));
}