繁体   English   中英

将 Map 的第一个元素添加到第一个 Position 的另一个列表中,依此类推...第二个 map 元素到第二个 Z4757FE07FD492ADBEEAZ6 列表中

[英]Add first element of Map into another list at 1st Position and so on…second map element into second position of list

**Here We have product bean. There is some attributes like productname,tax and ..etc.             ** 
Product product = new Product();
product.setProductName("Laptop");

Product product1 = new Product();
product1.setProductName("Mobile");
List<Product> productList = Arrays.asList(product, product1);
**Created Map of Map<String, List<Map<String, String>>>**
Map<String, List<Map<String, String>>> productCart = new HashMap<String, List<Map<String, String>>>();
List<Map<String, String>> listTax1 = new ArrayList<Map<String, String>>();
List<Map<String, String>> listTax2 = new ArrayList<Map<String, String>>();

Map<String, String> map1 = new HashMap<String, String>();
map1.put("XR", "123");
map1.put("TAX", "234");
map1.put("SURCHARGE", "567");
listTax1.add(map1);

Map<String, String> map2 = new HashMap<String, String>();
map2.put("XR", "1234");
map2.put("TAX", "2345");
map2.put("SURCHARGE", "5678");
listTax2.add(map2);

productCart.put("1", listTax1);
productCart.put("2", listTax2);

// I want to add productCart into productList one by one.// Add first element of Map into another list at 1st Position and so on...second map element into second position of list.

尝试使用 Java 8

是的,你可以使用这个 -

int j=0;
for(int i=0;i<productList.size();i++) {
    Product productData=  productList.get(i);
    List keys =new ArrayList(productCart.keySet());
    while(j<=i) {
        Object listKeys=  keys.get(i);
        List<Map<String, String>> pro=productCart.get(listKeys);
        for (Map<String, String> map : pro) {
                map.forEach((k,v)-> {
                   //Write Yours Conditions 
                    if(k.equalsIgnoreCase("SURCHARGE")){
                    }
                });

        }
        j++;
    }

}

暂无
暂无

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

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