1. Iterator遍历 EntrySet

/*** 在 Java 中遍历 HashMap 的5种最佳方法* @author Ramesh Fadatare**/
public class IterateHashMapExample {public static void main(String[] args) {// 1. 使用 Iterator 遍历 HashMap EntrySetMap < Integer, String > coursesMap = new HashMap < Integer, String > ();coursesMap.put(1, "C");coursesMap.put(2, "C++");coursesMap.put(3, "Java");coursesMap.put(4, "Spring Framework");coursesMap.put(5, "Hibernate ORM framework");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());}}
}

2. 使用 Iterator 遍历 HashMap KeySet


/*** 在 Java 中遍历 HashMap 的5种最佳方法* @author Ramesh Fadatare**/
public class IterateHashMapExample {public static void main(String[] args) {Map < Integer, String > coursesMap = new HashMap < Integer, String > ();coursesMap.put(1, "C");coursesMap.put(2, "C++");coursesMap.put(3, "Java");coursesMap.put(4, "Spring Framework");coursesMap.put(5, "Hibernate ORM framework");// 2. 使用 Iterator 遍历 HashMap KeySetIterator < Integer > iterator = coursesMap.keySet().iterator();while (iterator.hasNext()) {Integer key = iterator.next();System.out.println(key);System.out.println(coursesMap.get(key));}}
}

3. 使用 For-each 循环遍历 HashMap


/*** 在 Java 中遍历 HashMap 的5种最佳方法* @author Ramesh Fadatare**/
public class IterateHashMapExample {public static void main(String[] args) {Map < Integer, String > coursesMap = new HashMap < Integer, String > ();coursesMap.put(1, "C");coursesMap.put(2, "C++");coursesMap.put(3, "Java");coursesMap.put(4, "Spring Framework");coursesMap.put(5, "Hibernate ORM framework");// 3. 使用 For-each 循环遍历 HashMapfor (Map.Entry < Integer, String > entry: coursesMap.entrySet()) {System.out.println(entry.getKey());System.out.println(entry.getValue());}}
}

4. 使用 Lambda 表达式遍历 HashMap


/*** 在 Java 中遍历 HashMap 的5种最佳方法* @author Ramesh Fadatare**/
public class IterateHashMapExample {public static void main(String[] args) {Map < Integer, String > coursesMap = new HashMap < Integer, String > ();coursesMap.put(1, "C");coursesMap.put(2, "C++");coursesMap.put(3, "Java");coursesMap.put(4, "Spring Framework");coursesMap.put(5, "Hibernate ORM framework");// 4. 使用 Lambda 表达式遍历 HashMapcoursesMap.forEach((key, value) -> {System.out.println(key);System.out.println(value);});}
}
  1. 使用 Stream API 遍历 HashMap
/*** 在 Java 中遍历 HashMap 的5种最佳方法* @author Ramesh Fadatare**/
public class IterateHashMapExample {public static void main(String[] args) {Map < Integer, String > coursesMap = new HashMap < Integer, String > ();coursesMap.put(1, "C");coursesMap.put(2, "C++");coursesMap.put(3, "Java");coursesMap.put(4, "Spring Framework");coursesMap.put(5, "Hibernate ORM framework");// 5. 使用 Stream API 遍历 HashMapcoursesMap.entrySet().stream().forEach((entry) - > {System.out.println(entry.getKey());System.out.println(entry.getValue());});}
}

HashMap的五种遍历方法相关推荐

  1. 面试官:HashMap有几种遍历方法?推荐使用哪种?

    作者 | 磊哥 来源 | Java面试真题解析(ID:aimianshi666) 转载请联系授权(微信ID:GG_Stone) HashMap 的遍历方法有很多种,不同的 JDK 版本有不同的写法,其 ...

  2. 【HashMap】HashMap的6种遍历方法

    目录 1 创建map 2 keySet获取Map集合key的集合 然后在遍历key即可 3 通过Map.entrySet遍历key和value, 4 通过迭代器(Iterator)的方式 5 分别循环 ...

  3. HashMap的四种遍历方法,及效率比较(简单明了)

    public static void main(String[] args) {HashMap<Integer, String> map = new HashMap<Integer, ...

  4. java五种遍历HashMap的方法和性能分析

    在本文中,我们将通过示例讨论在 Java 上遍历 HashMap 的五种最佳方法. 使用Iterator迭代 使用 For-each + entrySet 循环遍历 HashMap 使用 For-ea ...

  5. HashMap 的 7 种遍历方式与性能分析!(强烈推荐)

    来自:Java中文社群 随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历 ...

  6. java map集合遍历方法,Java的Map集合的三种遍历方法

    集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ 1. package com.myTest.MapText; import java.util.Collection; i ...

  7. HashMap 的 7 种遍历方式与性能分析!「修正篇」

    这是我的第 57 篇原创文章 首先,给大家说声抱歉~ 事情经过是这样子的,五一节前我发布了一篇文章<HashMap 的 7 种遍历方式与性能分析!>,但是好心的网友却发现了一个问题,他说 ...

  8. HashMap 的 7 种遍历方式与性能分析!

    随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历方法讲起,然后再从性能. ...

  9. HashMap 的 7 种遍历方式+性能分析!

    随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历方法讲起,然后再从性能. ...

最新文章

  1. 动态规划—最长公共子序列问题 HDU-1159 Common Subsequence
  2. 博客系统如何随机插入大量文章数据
  3. 多系统并行服务器,具有分布式并行I/O接口的分布式并行服务器系统的性能研究...
  4. 第二个冲刺期的第四天
  5. c语言:使用main函数的参数,实现一个整数计算器
  6. JDK1.7 API -- Scanner
  7. 力扣题目系列:1299. 将每个元素替换为右侧最大元素 -- 一道算法优化入门题
  8. 音效php源码,音效大全mp3_音频素材下载(19999款音效素材包)
  9. 本征频率有时也称为特征频率,固有频率,本振频率
  10. doodoo.js快速入门教程 1
  11. git 回退远端master/develop分支版本
  12. LeetCode刷题记
  13. 几个数字的组合方式种类个数
  14. Word一些功能的使用方法
  15. docker containers status显示Removal In Progress
  16. Uva 12627 Erratic Expansion(不稳定膨胀)
  17. Javascript 模板模式
  18. vue热敏打印机_vue.js实现连接打印机
  19. python验证角谷_6.4验证角谷猜想
  20. 1 阿里物联网官方套件

热门文章

  1. Python入门到精通三天速成第二讲——类与继承
  2. 机器学习领域综述大列表:真的很大, 你忍一忍
  3. java lucene cms_JEECMSv6标签使用之[@cms_lucene_list]
  4. pte模拟考试_PTE猩际PC版-PTE猩际电脑版下载 v5.6.1--PC6电脑版
  5. 跨境电商独立站,该如何去经营呢?
  6. linux下ftp服务和dns的关系,linux企业常用服务---dns+ftp+dhcp
  7. Star Schema完全参考手册读书笔记二
  8. ArcSegment对象
  9. git bash linux 命令,Git Bash的妙用 - 使用Linux命令
  10. excel 单元格名称 java_Java 创建、编辑、删除Excel命名区域