前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一道有趣的Map迭代题

一道有趣的Map迭代题

作者头像
神秘的寇先森
发布2018-05-30 16:35:40
6160
发布2018-05-30 16:35:40
举报
文章被收录于专栏:Java进阶之路

需求:一个map,key为String类型,value为Integer类型,value保存了10个数字,其中有小于10的,有大于10的,设计一种方法返回把map中大于10的数字都删除的map。

代码语言:javascript
复制
package cn.baidu.map.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapRemoveDemo {
    public static void main(String[] args) {
        
        Map<String, Integer> map=new HashMap<>();
        map.put("A", 1);
        map.put("B", 5);
        map.put("C", 6);
        map.put("D", 7);
        map.put("E", 10);
        map.put("F", 11);
        map.put("G", 21);
        map.put("H", 31);
        map.put("I", 14);
        map.put("K", 15);
        map.put("L", 16);
        Set<String> keySet1 = map.keySet();
        System.out.print("执行方法前Map遍历结果");
        for (String key : keySet1) {
            System.out.print(map.get(key)+"---");
        }
        //function1(map);
        map = function3(map);
        Set<String> keySet2 = map.keySet();
        System.out.println();
        System.out.print("执行方法后Map遍历结果");
        for (String key : keySet2) {
            System.out.print(map.get(key)+"---");
        }
    }
    
    /**
     * 静态方法一:参数map,返回值map
     */
    public static Map<String, Integer> function1(Map<String, Integer> map){
        Set<Entry<String,Integer>> entrySet = map.entrySet();
        //创建一个集合把map中所有大于10的value的key装到list中
        List<String> list=new ArrayList<>();
        for (Entry<String, Integer> entry : entrySet) {
            if(entry.getValue()>10){
                list.add(entry.getKey());
            }
        }
        //遍历List,把list中的元素作为map中的key,执行删除方法
        for (String string : list) {
            map.remove(string);
        }
        return map;
    }
    
    /**
     * 静态方法2,直接在map迭代里删除
     * @param map
     * @return
     */
    public static Map<String, Integer> function2(Map<String, Integer> map){
        Set<Entry<String,Integer>> entrySet = map.entrySet();
        for (Entry<String, Integer> entry : entrySet) {
            if(entry.getValue()>10){
                map.remove(entry.getKey());
            }
        }
        return map;
    }
    
    /**
     * 方法3
     * @param map
     * @return
     */
    public static Map<String, Integer> function3(Map<String, Integer> map){
        Set<Entry<String,Integer>> entrySet = map.entrySet();
        Map<String, Integer> newmap=new HashMap<>();
        for (Entry<String, Integer> entry : entrySet) {
            if(entry.getValue()<=10){
                newmap.put(entry.getKey(), entry.getValue());
            }
        }
        return newmap;
    }
}

方法1是把map中的元素遍历出来,将value>10的元素的key装到一个list里面,然后在遍历list的过程中对每一个元素执行map.remove(key)方法。 方法3是重新创建一个Map,将原来的map遍历出来,将value<=1的map的key和value装到新map中,最后返回新创建的map。 其中方法1和方法3都能执行成功,如图

1.png

方法2执行失败,如图

function2.png

会发生并发修改异常,是因为在迭代的时候操作了集合,而map很关键的一点就是:不能在迭代过程中执行集合的任何方法。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018.01.29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档