zoukankan      html  css  js  c++  java
  • Java8 Map的遍历方式

    在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行。

    public class LambdaMap {
    
        private Map<String, Object> map = new HashMap<>();
    
        @Before
        public void initData() {
            map.put("key1", "value1");
            map.put("key2", "value2");
            map.put("key3", "value3");
            map.put("key4", 4);
            map.put("key5", 5);
            map.put("key5", 'h');
        }
    
    
        /**
         * 遍历Map的方式一
         * 通过Map.keySet遍历key和value
         */
        @Test
        public void testErgodicWayOne() {
            System.out.println("---------------------Before JAVA8 ------------------------------");
            for (String key : map.keySet()) {
                System.out.println("map.get(" + key + ") = " + map.get(key));
            }
            System.out.println("---------------------JAVA8 ------------------------------");
            map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
        }
    
        /**
         * 遍历Map第二种
         * 通过Map.entrySet使用Iterator遍历key和value
         */
        @Test
        public void testErgodicWayTwo() {
            System.out.println("---------------------Before JAVA8 ------------------------------");
            Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entry = iterator.next();
                System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
            }
            System.out.println("---------------------JAVA8 ------------------------------");
            map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));
        }
    
        /**
         * 遍历Map第三种
         * 通过Map.entrySet遍历key和value,在大容量时推荐使用
         */
        @Test
        public void testErgodicWayThree() {
            System.out.println("---------------------Before JAVA8 ------------------------------");
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
            }
            System.out.println("---------------------JAVA8 ------------------------------");
            map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
        }
    
        /**
         * 遍历Map第四种
         * 通过Map.values()遍历所有的value,但不能遍历key
         */
        @Test
        public void testErgodicWayFour() {
            System.out.println("---------------------Before JAVA8 ------------------------------");
            for (Object value : map.values()) {
                System.out.println("map.value = " + value);
            }
            System.out.println("---------------------JAVA8 ------------------------------");
            map.values().forEach(System.out::println); // 等价于map.values().forEach(value -> System.out.println(value));
        }
    
        /**
         * 遍历Map第五种
         * 通过k,v遍历,Java8独有的
         */
        @Test
        public void testErgodicWayFive() {
            System.out.println("---------------------Only JAVA8 ------------------------------");
            map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
        }
    }
  • 相关阅读:
    通过点击切换文本框内容的脚本示例
    使用脚本改变树控件的行为
    javascript动态创建radio button元素支持IE/Firefox
    轻量级的向导控件MultiView
    客户端脚本简单实现Repeater的无刷新分页
    在非web site项目中引用Membership
    逐步认识C#四种判断相等的方法
    C#获取csv文件内容对逗号和引号分隔的处理
    JavaScript之 值类型 和 引用类型 Better
    JS call apply bind 方法的区别 Better
  • 原文地址:https://www.cnblogs.com/jtlgb/p/8528085.html
Copyright © 2011-2022 走看看