Java_遍历 HashMap 的 5 种最佳方式

本文详细介绍了五种在Java中高效遍历HashMap的方法,包括使用Iterator遍历EntrySet和KeySet,利用For-each循环,Lambda表达式以及StreamAPI进行迭代,为开发者提供实用的代码示例。

在本文中,我们将通过示例讨论在 Java 上遍历 HashMap 的五种最佳方法。

  1. 使用 Iterator 遍历 HashMap EntrySet
  2. 使用 Iterator 遍历 HashMap KeySet
  3. 使用 For-each 循环迭代 HashMap
  4. 使用 Lambda 表达式遍历 HashMap
  5. 使用 Stream API 遍历 HashMap
1. 使用 Iterator 遍历 HashMap EntrySet
/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author test
 *
 */  
public class IterateHashMapExample {
    public static void main(String[] args) {
        // 1. 使用 Iterator 遍历 HashMap EntrySet
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "Java");
        coursesMap.put(2, "NET");
        coursesMap.put(3, "bootstrap");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "angular");

        Iterator < Entry < Integer, String >> iterator = coursesMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry < Integer, String > entry = iterator.next();
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

Output:

1
Java
2
NET
3
bootstrap
4
Spring Framework
5
angular
2. 使用 Iterator 遍历 HashMap KeySet
/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author test
 *
 */  
public class IterateHashMapExample {
    public static void main(String[] args) {
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "Java");
        coursesMap.put(2, "NET");
        coursesMap.put(3, "bootstrap");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "angular");

        // 2. 使用 Iterator 遍历 HashMap KeySet
        Iterator < Integer > iterator = coursesMap.keySet().iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.println(key);
            System.out.println(coursesMap.get(key));
        }
    }
}

Output:

1
Java
2
NET
3
bootstrap
4
Spring Framework
5
angular
3. 使用 For-each 循环遍历 HashMap
/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author test
 *
 */  
public class IterateHashMapExample {
    public static void main(String[] args) {
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "Java");
        coursesMap.put(2, "NET");
        coursesMap.put(3, "bootstrap");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "angular");

        // 3. 使用 For-each 循环遍历 HashMap
        for (Map.Entry < Integer, String > entry: coursesMap.entrySet()) {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}

Output:

1
Java
2
NET
3
bootstrap
4
Spring Framework
5
angular
4. 使用 Lambda 表达式遍历 HashMap
/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author test
 *
 */  
public class IterateHashMapExample {
    public static void main(String[] args) {
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "Java");
        coursesMap.put(2, "NET");
        coursesMap.put(3, "bootstrap");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "angular");

        // 4. 使用 Lambda 表达式遍历 HashMap
        coursesMap.forEach((key, value) -> {
            System.out.println(key);
            System.out.println(value);
        });
    }
}

Output:

1
Java
2
NET
3
bootstrap
4
Spring Framework
5
angular
5. 使用 Stream API 遍历 HashMap
/**
 * 在 Java 中遍历 HashMap 的5种最佳方法
 * @author test
 *
 */  
public class IterateHashMapExample {
    public static void main(String[] args) {
        Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
        coursesMap.put(1, "Java");
        coursesMap.put(2, "NET");
        coursesMap.put(3, "bootstrap");
        coursesMap.put(4, "Spring Framework");
        coursesMap.put(5, "angular");

        // 5. 使用 Stream API 遍历 HashMap
        coursesMap.entrySet().stream().forEach((entry) - > {
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        });
    }
}

Output:

1
Java
2
NET
3
bootstrap
4
Spring Framework
5
angular
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值