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

这里讲对List<Map<String, Object>>的数据结构的添加和删除实例

 

添加 

       //初始化        
        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();
           }
                        
        }

 ---------- 谢谢大佬打赏-----------

 

posted @ 2018-09-05 22:59  抽象Java  阅读(10403)  评论(0编辑  收藏  举报