遍历hashmap的6种方法
1、 通过ForEach循环进行遍历
mport java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class Test {
public static void main(String[] args) throws IOException {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 10);
map.put(2, 20); // Iterating entries using a For Each loop
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
} }
}
2、 ForEach迭代键值对方式
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class Test {
public static void main(String[] args) throws IOException {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 10);
map.put(2, 20); // 迭代键
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
} // 迭代值
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
}
}
3、使用带泛型的迭代器进行遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; public class Test {
public static void main(String[] args) throws IOException {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 10);
map.put(2, 20); Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
}
4、使用不带泛型的迭代器进行遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; public class Test { public static void main(String[] args) throws IOException { Map map = new HashMap();
map.put(1, 10);
map.put(2, 20); Iterator<Map.Entry> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer) entry.getKey();
Integer value = (Integer) entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
}
}
5、通过Java8 Lambda表达式遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class Test { public static void main(String[] args) throws IOException { Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 10);
map.put(2, 20);
map.forEach((k, v) -> System.out.println("key: " + k + " value:" + v));
}
}
6、使用 Stream API 遍历 HashMap
package com.java.tutorials.iterations; import java.util.HashMap;
import java.util.Map; /**
* 在 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 遍历 HashMap
coursesMap.entrySet().stream().forEach((entry) - > {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
});
}
}
遍历hashmap的6种方法的更多相关文章
- 遍历hashmap 的四种方法
以下列出四种方法 public static void main(String[] args) { Map<String,String> map=new HashMap<String ...
- 遍历HashMap的四种方法
public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...
- 转载:遍历Map的四种方法
http://www.cnblogs.com/kristain/articles/2033566.html 遍历Map的四种方法 public static void main(String[] ar ...
- java 遍历map的四种方法
16:21:42 Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是Map中的一个接口,他的用途是表示一个映射项( ...
- 遍历python字典几种方法
遍历python字典几种方法 from: http://ghostfromheaven.iteye.com/blog/1549441 aDict = {'key1':'value1', 'key2': ...
- Java遍历集合的几种方法
遍历集合的几种方法 用不同的方法遍历集合. public interface Iterator:对Collection进行迭代的迭代器.迭代器取代了Java Collections FrameWork ...
- 遍历map的四种方法
方法一 在for-each循环中使用entries来遍历这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用.注意:for-each循环在Java 5中被引入所以该方法只能应用于j ...
- java中遍历map的几种方法介绍
喜欢用Java写程序的朋友都知道,我们常用的一种数据结构map中存储的是键值对,我们一般存储的方式是: map.put(key, value); 而提取相应键的值用的方法是: map.ge ...
- java 遍历Map的4种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
随机推荐
- [笔记] encoder-decoder NEURAL MACHINE TRANSLATION BY JOINTLY LEARNING TO ALIGN AND TRANSLATE
原文地址 :[1409.0473] Neural Machine Translation by Jointly Learning to Align and Translate (arxiv.org) ...
- Python旋转魔方阵
[问题描述]输入一个自然数N(2≤N≤9),要求输出如下的魔方阵,即边长为N*N,元素取值为1至N*N,1在左上角,呈顺时针方向依次放置各元素. N=3时: 1 2 3 8 ...
- HTTPS 握手过程理解
转自https://www.jianshu.com/p/a3a25c6627ee https://blog.csdn.net/xingtian713/article/details/11953057 ...
- SQL优化一例:通过改变分组条件(减少计算次数)来提高效率
#与各人授权日期相关,所以有十万用户,就有十万次查询(相关子查询) @Run.ExecuteSql("更新各人应听正课数",@"update bi_data.study_ ...
- 在react项目中实现表格导出为Excel
需求背景 数据表格有时需要增加导出Excel功能,大多数情况下都是后端出下载接口,前端去调用. 对于数据量少的数据,可以通过前端技术实现,减少后端工作. 实现方式 使用插件--xlsx 根据自己项目情 ...
- windows使用natapp教程
这种免费的 有个缺点 过段时间就会连接不上,需要不定时更换域名地址 Linux系统参考:https://www.cnblogs.com/pxblog/p/10549847.html 官网地址:http ...
- 十行代码搭建基于.NET6三维应用程序
本文介绍在.NET6环境下如何集成Rapid SDK三维控件,请首先确保已经安装了Vistual Studio 2022,社区版就够用了. 1 创建项目 选择创建Windows窗体应用 给程序起一个酷 ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 15 - Vue3 UI Framework - 完工部署
项目官网也基本完成了,接下来我们再讲一下如何将项目官网部署到 GitHub Pages 上 返回阅读列表点击 这里 项目配置 常见的模式有三种,即 History 模式 Hash 模式 Memory ...
- Histogram Processing
目录 HISTOGRAM EQUALIZATION 代码示例 HISTOGRAM MATCHING (SPECIFICATION) 其它 Gonzalez R. C. and Woods R. E. ...