简体   繁体   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. // 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.

Try with Java 8尝试使用 Java 8

Yes you can use this one -是的,你可以使用这个 -

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.

相关问题 在 java 的两个列表中查找 Common 元素,并使用 java8 将它们添加到列表中的起始位置,第二个列表的剩余元素 - Find Common element in two lists in java and add them to starting position in list with remaining element of second list using java8 使用 Stream 将列表中的元素移动到第一个 position - Move an element in the list to the first position using Stream 对元素仍处于第一个位置的列表进行排序 - Sort a list with element still in first position 如何声明元素在列表中的位置(确认它是列表中的第一个元素) - How to assert position of element on the list (Confirm it is first element on the list) 更改元素在列表中的位置 - Change the position of element in List 如何在 java 的嵌套列表中获取每个列表的第一个和第二个元素 - How to get first and second element of a each list in a nested list in java Java从数组列表中获取第二个元素,并将其用作哈希映射的值 - Java getting every second element from array list and using it as value for hash map 如何添加List元素以按键映射? - How to add List element to map by key? 将第一个数组元素与最后一个交换,第二个与倒数第二个交换,依此类推 - Swapping first array element with last, second with second last and so on 向列表中添加第二个项目似乎会覆盖我添加的第一个元素 - Adding a second item to a list seems to override the first element I added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM