作为小白的我,突然在一个公众号里面看见了遍历HashMap的方法,居然一口气写出来五种,嘿嘿,只能把这个干货写入我的随笔中了,哈哈哈。
1、使用Ieterator遍历HashMap EntrySet

public class IterateHashMapTest {public static void main(String[] args) {Map<Integer, String> map = new HashMap<>();map.put(1,"A");map.put(2,"B");map.put(3,"C");map.put(4,"D");map.put(5,"E");map.put(6,"F");Iterator<Map.Entry<Integer,String>> iterator=map.entrySet().iterator();while (iterator.hasNext()){Map.Entry<Integer,String> entry=iterator.next();System.out.print(entry.getKey()+" ");System.out.println(entry.getValue());}}
}

2、使用Ieterator遍历HashMap KeySet

public class IterateHashMapTest {public static void main(String[] args) {Map<Integer, String> map = new HashMap<>();map.put(1,"A");map.put(2,"B");map.put(3,"C");map.put(4,"D");map.put(5,"E");map.put(6,"F");Iterator<Integer> iterator=map.keySet().iterator();while (iterator.hasNext()){Integer key=iterator.next();System.out.print(key+" ");System.out.println(map.get(key));}}
}

3、Foreach语句遍历

public class IterateHashMapTest {public static void main(String[] args) {Map<Integer, String> map = new HashMap<>();map.put(1,"A");map.put(2,"B");map.put(3,"C");map.put(4,"D");map.put(5,"E");map.put(6,"F");for (Map.Entry<Integer,String> entry : map.entrySet()){System.out.println(entry.getKey()+" "+entry.getValue());}}
}

4、Lambda表达式遍历HashMap

public class IterateHashMapTest {public static void main(String[] args) {Map<Integer, String> map = new HashMap<>();map.put(1,"A");map.put(2,"B");map.put(3,"C");map.put(4,"D");map.put(5,"E");map.put(6,"F");map.forEach((key,value)->{System.out.println(key+" "+value);});    }
}

5、使用Stream API 遍历HashMap

public class IterateHashMapTest {public static void main(String[] args) {Map<Integer, String> map = new HashMap<>();map.put(1,"A");map.put(2,"B");map.put(3,"C");map.put(4,"D");map.put(5,"E");map.put(6,"F");map.entrySet().stream().forEach((entry )->{System.out.println(entry.getKey()+" "+entry.getValue());});}
}

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

  1. 遍历hashMap的5种方法

    1.使用Iterator 遍历HashMap EntrySet 2.使用Iterator 遍历HashMap KeySet 3.使用For-each 循环迭代 HashMap 4.使用Lambda 表 ...

  2. 遍历hashmap 的四种方法

    以下列出四种方法 public static void main(String[] args) {Map<String,String> map=new HashMap<String, ...

  3. 遍历HashMap的几种方法

    有如下几种方法: 1. 通过keyset 2. 通过 Map.entrySet().iterator() 3. 通过foreach ---- Map.entryset, 当hashmap很大时,推荐使 ...

  4. Java遍历Map的五种方法(Java8中新lambda方法)

    遍历Map的方法, 我总结了5种, CoreJava中给出的是第一种使用forEach+lambda表达式, 我认为这种方法应该是最好的, 但是只能在Java8之后的版本使用; 建议优先使用第一种和第 ...

  5. java中遍历HashMap的四种方法及效率比较

    1. 推荐方法: 使用entrySet 遍历Map 类集合KV,而不是keySet 方式进行遍历. 代码示例如下: // 循环第二种HashMap<Integer, String> map ...

  6. HashMap 遍历hashMap的7种方法和删除数据两种方式

    遍历数据 1.使用Iterator 遍历HashMap EntrySet 2.使用Iterator 遍历HashMap KeySet 3.使用For-each EntrySet 循环迭代 HashMa ...

  7. java stream 遍历map_遍历map的五种方法介绍

    日常开发中Map集合遍历是经常使用的,下面介绍几种遍历方法的差异. (推荐教程:java课程) 1.Iterator+entrySet写法[推荐JDK8以下],Map.Entry是Map接口的内部接口 ...

  8. 遍历HashMap的四种方法

    在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet(): 将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键 ...

  9. 遍历HashMap的七种方法

    直接上代码 /*** @Description* @Author 暴怒阿西* @Date 2022-11-15 15:22*/ public class HashMapTest2 {static Ha ...

最新文章

  1. solrcloud集群搭建
  2. keyvaluepair_KeyValuePair用法(转)
  3. MYSQL-主键、外键
  4. 百度BML 图像分类-多标签在线API调试结果记录
  5. 速修复!热门代码覆盖率测试工具 Codecov 的脚本遭恶意修改,敏感信息被暴露...
  6. 优化理论08-----约束优化的最优性条件、拉格朗日条件、凸性、约束规范、二阶最优性条件(下)
  7. 汇编语言·易遗忘知识点汇总ing
  8. linux下运行vasp,VASP5.3.5 并行版本+VTST从头编译教程(完整版)
  9. 大数据分析案例:财政收入预测分析
  10. 运行ug软件需要什么样的配置?
  11. 有原函数,可积、变限积分
  12. html5 生成条码,Html5添加Canvas的EAN13条形码生成插件教程
  13. 期货权益可用资金(期货的权益和可用资金)
  14. L1-020 帅到没朋友 (20 分)
  15. 天蝎座2005年运势
  16. Swift3 - String 字符串、Array 数组、Dictionary 字典的使用
  17. 基于微信小程序的流动人口管理移动APP设计与实现-计算机毕业设计源码+LW文档
  18. Zabbix之SNMP部署心得
  19. 如何在本机上拥有虚拟机软件(Linux系统)(如何在本机上安装虚拟镜像)
  20. IT人的晋升之路——关于人际交往能力的培养

热门文章

  1. 字体图标、文字、背景色的css渐变
  2. VS2017 CUDA编程学习5:CUDA并行执行-线程
  3. 在线给头像戴上绿帽子网站源码
  4. Android telephone
  5. 某联招聘版某数反反爬— 2. 总体剖析
  6. 计算机网络 - 常见的网络协议
  7. oracle数据库中最常用的sql语句
  8. 扩展ps流增加“视频帧录像时刻绝对时间戳”方法
  9. Caused by: org.flywaydb.core.api.FlywayException: Validate failed: Migration checksum mismatch for m
  10. jsrender的基本使用