java集合

map集合遍历
 Map<String,Object> map = new HashMap<>();
        map.put("a","aaaa");
        map.put("b","bbbb");
        map.put("c","cccc");
        //高效率遍历Map以及在循环过程中移除 remove指定key
        //使用iter循环的时候 可以在循环中移除key,for在循环的过程中移除会报错哦
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()){
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            String curKey;
            if(key!=null){
                curKey = key.toString();
                if("a".equals(curKey)){
                    iter.remove();//移除
                }
            }
        }
        for (Map.Entry<String,Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            System.out.println("key:"+key+",vlaue:"+value);
        }

  

posted @ 2023-03-31 15:01  a快乐码农  阅读(8)  评论(0编辑  收藏  举报