zoukankan      html  css  js  c++  java
  • 03Java8Map遍历

    Java8Map的遍历

    遍历方式

    package com.arithmetic;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    public class Java8Map {
    
        public static void main(String[] args) {
    
            Map<String, Object> map = new HashMap<>();
            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
             */
            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
             */
            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,在大容量时推荐使用
             */
            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
             */
            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第五种
             * 通过k,v遍历,Java8独有的
             */
            System.out.println("---------------------Only JAVA8 ------------------------------");
            map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
        }
        
    }
    
    

    推荐使用:

    • 遍历Map第三种 entrySet().forEach

    • 遍历Map第五种 forEach

      HashMap使用的是数组加链表的方式存储数据

      函数式编程方式lamada表达式

  • 相关阅读:
    python学习之ajax和可视化管理工具
    操作系统-保护模式中的特权级下
    redis 分布式锁的 5个坑,真是又大又深
    数据库之数据表控制语句
    【NoSQL】Consul中服务注册的两种方式
    netstat命令使用方法以及详解
    Dockerfile与Dockerfile实战
    Spring boot+redis实现消息发布与订阅
    怎么寻回位置不可用移动硬盘的数据
    python字符前面u,r,f等含义
  • 原文地址:https://www.cnblogs.com/mzyc/p/15797522.html
Copyright © 2011-2022 走看看