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

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 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> 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 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 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 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 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种最佳方式!)