1. 使用 Iterator 遍历 HashMap EntrySet

2. 使用 Iterator 遍历 HashMap KeySet

3. 使用 For-each 循环迭代 HashMap

4. 使用 Lambda 表达式遍历 HashMap

5. 使用 Stream API 遍历 HashMap

显示例子:

1. 使用 Iterator 遍历 HashMap EntrySet

package com.jia.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; public class HashMapDemo { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>();
map.put(1,"java");
map.put(2,"c");
map.put(3,"c++");
map.put(4,"c#");
map.put(5,"spring");
    
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<Integer, String> entry = iterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue()); }
}
}

Output:

1:java
2:c
3:c++
4:c#
5:spring Process finished with exit code 0

2. 使用 Iterator 遍历 HashMap KeySet

package com.jia.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; public class HashMapDemo02 { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>();
map.put(1,"java");
map.put(2,"c");
map.put(3,"c++");
map.put(4,"c#");
map.put(5,"spring"); Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()){
Integer key = iterator.next();
System.out.println(key);
System.out.println(map.get(key));
}
}
}

Output:

1
java
2
c
3
c++
4
c#
5
spring Process finished with exit code 0

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

package com.jia.map;

import java.util.HashMap;
import java.util.Map; public class HashMapDemo03 { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>();
map.put(1,"java");
map.put(2,"c");
map.put(3,"c++");
map.put(4,"c#");
map.put(5,"spring"); for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
}
}

Output:

1:java
2:c
3:c++
4:c#
5:spring Process finished with exit code 0

4. 使用 Lambda 表达式遍历 HashMap(推荐!!!

package com.jia.map;

import java.util.HashMap;
import java.util.Map; public class HashMapDemo04 { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>();
map.put(1,"java");
map.put(2,"c");
map.put(3,"c++");
map.put(4,"c#");
map.put(5,"spring"); map.forEach((key,value) ->{
System.out.println(key + ":" + value);
});
}
}

Output:

1:java
2:c
3:c++
4:c#
5:spring Process finished with exit code 0

5. 使用 Stream API 遍历 HashMap(推荐!!!

package com.jia.map;

import java.util.HashMap;
import java.util.Map; public class HashMapDemo05 { public static void main(String[] args) { Map<Integer,String> map = new HashMap<>();
map.put(1,"java");
map.put(2,"c");
map.put(3,"c++");
map.put(4,"c#");
map.put(5,"spring"); map.entrySet().stream().forEach((entry) ->{
System.out.println(entry.getKey() + ":" + entry.getValue());
});
}
}

Output:

1:java
2:c
3:c++
4:c#
5:spring Process finished with exit code 0

HashMap集合类 5种最佳遍历方式的更多相关文章

  1. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  2. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)

    原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ...

  3. 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...

  4. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]

    Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...

  5. (转)ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  6. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

  7. JS几种数组遍历方式以及性能分析对比

    前言 这一篇与上一篇 JS几种变量交换方式以及性能分析对比 属于同一个系列,本文继续分析JS中几种常用的数组遍历方式以及各自的性能对比 起由 在上一次分析了JS几种常用变量交换方式以及各自性能后,觉得 ...

  8. JS几种数组遍历方式总结

    JS数组遍历的几种方式 JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代 ...

  9. 遍历 HashMap 的 5 种最佳方式

    使用 Iterator 遍历 HashMap EntrySet 使用 Iterator 遍历 HashMap KeySet 使用 For-each 循环迭代 HashMap 使用 Lambda 表达式 ...

随机推荐

  1. x86架构中的外部中断结构-Part 1:中断控制器的演化

    本文主要讲解了x86体系架构从外部设备接受中断的过程,本文是系列文章的第一部分,试图回答以下问题: 什么是PIC以及它的用途是什么? 什么是APIC以及它的用途是什么?LAPIC和I/O APIC的目 ...

  2. mysql数据库 Window下安装

    关系数据库,是建立在关系数据库模型基础上的数据库,借助于集合代数等概念和方法来处理数据 库中的数据,同时也是一个被组织成一组拥有正式描述性的表格,该形式的表格作用的实质是装载着数 据项的特殊收集体,这 ...

  3. Java并发基础之AbstractQueuedSynchronizer(AQS)

    AbstractQueuedSynchronizer同步器是实现JUC核心基础组件,因为 定义了一套多线程访问共享资源的同步器框架.前面几篇文章中JUC同步工具中都利用AQS构建自身的阻塞类.AQS解 ...

  4. swagger 2.0

    1.引入jar包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox- ...

  5. Hive复杂数组字典(Json-Array)解析

    数据存储字段格式如下(Json-Array互相嵌套): string='{"id":"9088848902695992720","title" ...

  6. linux中rlwrap安装

    转至:https://www.cnblogs.com/hw-1015/p/6601294.html 在linux上使用sqlplus命令的时候,上下键.空格键.删除键都不能使用,非常麻烦.安装了rlw ...

  7. parquet列存储本身自带压缩 配合snappy或者lzo等可以进行二次压缩

    上传txt文件到hdfs,txt文件大小是74左右. 这里提醒一下,是不是说parquet加lzo可以把数据压缩到这个地步,因为我的测试数据存在大量重复.所以下面使用parquet和lzo的压缩效果特 ...

  8. JAVA String、StringBuilder、和StringBuffer的区别,及如何使用

    目录 String类 一.String类的理解和创建对象 二.String类创建的方式 两种创建String对象的区别 测试题 三.String常用方法 四.StringBuffer类 1.Strin ...

  9. Chapter02 Java概述

    Chapter02 Java概述 目录 Chapter02 Java概述 2.1 什么是程序 程序: 2.2 Java的重要特点 2.3 Java 运行机制及运行过程 2.3.1 Java 语言的特点 ...

  10. JZ-019-顺时针打印矩阵

    顺时针打印矩阵 题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4 矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 ...