美文网首页
Java中遍历 HashMap 的5种最佳方式!

Java中遍历 HashMap 的5种最佳方式!

作者: Coding测试 | 来源:发表于2020-07-16 22:58 被阅读0次
    image

    HashMap五种遍历方式

    本文通过示例演示 Java 上遍历 HashMap的五种最佳方法。使用 Iterator 遍历 HashMap EntrySet使用 Iterator 遍历 HashMap KeySet使用 For-each 循环迭代 HashMap使用 Lambda 表达式遍历 HashMap使用 Stream API 遍历 HashMap

    1、使用Iterator遍历HashMap EntrySet

    示例

     @Test
        public void entrySetTraverse(){
            Map<Integer,String> map = new HashMap<>();
            map.put(1,"java");
            map.put(2,"python");
            map.put(3,"C");
            map.put(4,"c++");
            map.put(5,"go");
            // 1. 使用 Iterator 遍历 HashMap EntrySet
            Iterator<Map.Entry<Integer, String>> integrator = map.entrySet().iterator();
            while (integrator.hasNext()){
                Map.Entry< Integer, String > entry = integrator.next();
                System.out.println(entry.getKey());
                System.out.println(entry.getValue());
            }
    
        }
    

    输出

    1
    java
    2
    python
    3
    C
    4
    c++
    5
    go
    ===============================================
    Default Suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================
    

    2、使用Iterator遍历HashMap KeySet

    示例

    @Test
        public  void keySetTraverse() {
            Map<Integer,String> map = new HashMap<>();
            map.put(1,"java");
            map.put(2,"python");
            map.put(3,"C");
            map.put(4,"c++");
            map.put(5,"go");
    
            // 2. 使用 Iterator 遍历 HashMap KeySet
            Iterator < Integer > iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                Integer key = iterator.next();
                System.out.println(key);
                System.out.println(map.get(key));
            }
        }
    

    输出

    1
    java
    2
    python
    3
    C
    4
    c++
    5
    go
    ===============================================
    Default Suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================
    

    3、使用foreach(增强for)遍历HashMap(常用)

    示例

    @Test
        public  void foreachTraverse() {
            Map<Integer,String> map = new HashMap<>();
            map.put(1,"java");
            map.put(2,"python");
            map.put(3,"C");
            map.put(4,"c++");
            map.put(5,"go");
    
            // 3. 使用 For-each(增加for) 循环遍历 HashMap
            for (Map.Entry < Integer, String > entry: map.entrySet()) {
                System.out.println(entry.getKey());
                System.out.println(entry.getValue());
            }
        }
    

    输出

    1
    java
    2
    python
    3
    C
    4
    c++
    5
    go
    ===============================================
    Default Suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================
    

    4、使用Lambda表达式遍历HashMap(常用)

    示例(Lambda表达虽然常用但是编辑的性能没有增加for好)

    @Test
        public static void lambdaTraverse() {
            Map<Integer,String> map = new HashMap<>();
            map.put(1,"java");
            map.put(2,"python");
            map.put(3,"C");
            map.put(4,"c++");
            map.put(5,"go");
    
            // 4. 使用 Lambda 表达式遍历 HashMap
            map.forEach((key, value) -> {
                System.out.println(key);
                System.out.println(value);
            });
        }
    

    输出

    1
    java
    2
    python
    3
    C
    4
    c++
    5
    go
    ===============================================
    Default Suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================
    

    5、使用Stream API遍历HashMap

    示例

     @Test
        public static void streamTraverse() {
            Map<Integer,String> map = new HashMap<>();
            map.put(1,"java");
            map.put(2,"python");
            map.put(3,"C");
            map.put(4,"c++");
            map.put(5,"go");
    
            // 5. 使用 Stream API 遍历 HashMap
            map.entrySet().stream().forEach((entry) -> {
                System.out.println(entry.getKey());
                System.out.println(entry.getValue());
            });
        }
    

    输出

    1
    java
    2
    python
    3
    C
    4
    c++
    5
    go
    ===============================================
    Default Suite
    Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
    ===============================================
    

    相关文章

      网友评论

          本文标题:Java中遍历 HashMap 的5种最佳方式!

          本文链接:https://www.haomeiwen.com/subject/gvcphktx.html