HashMap集合类 5种最佳遍历方式
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种最佳遍历方式的更多相关文章
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)
原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ...
- 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析
原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...
- Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]
Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...
- (转)ArrayList和LinkedList的几种循环遍历方式及性能对比分析
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...
- JS几种数组遍历方式以及性能分析对比
前言 这一篇与上一篇 JS几种变量交换方式以及性能分析对比 属于同一个系列,本文继续分析JS中几种常用的数组遍历方式以及各自的性能对比 起由 在上一次分析了JS几种常用变量交换方式以及各自性能后,觉得 ...
- JS几种数组遍历方式总结
JS数组遍历的几种方式 JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代 ...
- 遍历 HashMap 的 5 种最佳方式
使用 Iterator 遍历 HashMap EntrySet 使用 Iterator 遍历 HashMap KeySet 使用 For-each 循环迭代 HashMap 使用 Lambda 表达式 ...
随机推荐
- n次单位根(n-th unit root)
最近在看CKKS方案,里面的编码/解码用到了n次单位根,感觉基于环上的加密,很多都会用到,现在系统的学习一下! 定义 先看定义: \[z^n=1,(n=1,2,3,...) \] 该方程的根z为n次单 ...
- leetcode算法1.两数之和
哈喽!大家好,我是[学无止境小奇],一位热爱分享各种技术的博主! [学无止境小奇]的创作宗旨:每一条命令都亲自执行过,每一行代码都实际运行过,每一种方法都真实实践过,每一篇文章都良心制作过. [学无止 ...
- Spring Cloud Feign 如何使用对象参数
概述 Spring Cloud Feign 用于微服务的封装,通过接口代理的实现方式让微服务调用变得简单,让微服务的使用上如同本地服务.但是它在传参方面不是很完美.在使用 Feign 代理 GET 请 ...
- dart系列之:集合使用最佳实践
目录 简介 使用字面量创建集合 不要使用.length来判断集合是否为空 可遍历对象的遍历 List.from和iterable.toList where和whereType 避免使用cast 总结 ...
- KVM 虚机镜像操作, 扩容和压缩
KVM镜像操作 qemu-img命令 创建镜像 qemu-img create # 创建一个设备空间大小为10G的镜像 qemu-img create -f qcow2 centos7-guest.q ...
- XSS Challenge靶场练习
实验目的 学习xss的基础知识及利用方式. 实验原理 XSS 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩写 ...
- scrapy的介绍、组件、数据流
scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,我们只需要实现少量代码,就能够快速的抓取到数据内容. scrapy使用了twisted异步网络框架来处理网络通讯,来加快我们的下载速 ...
- iOS 通知扩展插件
iOS 通知扩展插件 目录 iOS 通知扩展插件 Notification Service Extension 新建一个target 代码实现 注意事项 UINotificationConentExt ...
- node热加载
node可以通过require热加载文件,这里先提一下require的加载方式: 当我们第一次使用require加载模块时require会把被加载文件的绝对路径作为key存放在require的cach ...
- Pandas:loc iloc ix用法
参考:Pandas中关于 loc \ iloc \ ix 用法的理解 相同点 使用形式都是 df.xxx[ para1 , para2 ] #xxx表示loc iloc ix#df表示一个DataFr ...