1. 程式人生 > >添加,刪除List<Map<String, Object>>元素

添加,刪除List<Map<String, Object>>元素

service content nbsp string 初始化 tof arr n) con

添加

       //初始化        
        List<Map<String, Object>> products = new ArrayList<Map<String,Object>>();
        //也可以這樣初始化List<Map<String, Object>> products = null
        
        //初始Map<String,Object>
        Map<String,Object> product01 = null;            
        product01.put(
"Parameter01", "Key1"); Map<String,Object> product02 = null; String parameter02 = "Parameter02"; String key2 = "theContentOfKey2"; product02.put(parameter02, key2); //一個一個添加 products.add(product01); products.add(product02);
//從Service中獲取 List<Map<String, Object>> productsAddition = productService.getProduct(); //遍歷添加 for(Map<String, Object> product: productsAddition){ products.add(product); }

刪除--使用叠代刪除

          //初始化
         List<Map<String, Object>> products =  null
; //獲取List元素 products = productService.getProduct(); //使用叠代刪除 Iterator<Map<String, Object>> productsIterator = products.iterator(); while (productsIterator.hasNext()) { //刪除特定元素 Map<String, Object> product = productsIterator.next(); if("product01".equals(product.get("productName"))){ productsIterator.remove(); } if("product02".equals(product.get("productName"))){ productsIterator.remove(); } }

添加,刪除List<Map<String, Object>>元素