码迷,mamicode.com
首页 > 编程语言 > 详细

Java面试题

时间:2017-03-11 23:07:27      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:sys   class   val   blog   entry   get   st3   map   equals   

两个map,循环一次取出key,value都相等的交集

        Map<String, String> map1 = new HashMap<>();
        map1.put("test1", "test");
        map1.put("test2", "test2");
        map1.put("test3", "test3");
        map1.put("test4", "test4");
        map1.put("test5", "test");
        System.out.println("mp1:" + map1);

        Map<String, String> map2 = new HashMap<>();
        map2.put("test6", "test6");
        map2.put("test1", "test7");
        map2.put("test8", "test8");
        map2.put("test4", "test");
        map2.put("test5", "test");
        map2.put("test9", "test1");
        System.out.println("mp2:" + map2);

        Map<String, String> map3 = new HashMap<>();

        Iterator iteratorTemp = map1.entrySet().iterator();
        while (iteratorTemp.hasNext()) {
            Map.Entry entry = (Entry) iteratorTemp.next();
            System.out.println(entry);
            if (map2.containsKey(entry.getKey())
                    && entry.getValue().equals(map2.get(entry.getKey()))) {
                map3.put((String) entry.getKey(), (String) entry.getValue());
            }
        }

        System.out.println("一次循环map1和map2的交集为:" + map3);

 

Java面试题

标签:sys   class   val   blog   entry   get   st3   map   equals   

原文地址:http://www.cnblogs.com/xiaozi-autotestman/p/6536545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!