遍历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都 ...
随机推荐
- 远程调用RPC
一.简介 RPC,就是Remote Procedure Call的简称呀,翻译成中文就是远程过程调用. 本地调用,就好比你现在在家里,你要想洗碗,那你直接把碗放进洗碗机,打开洗碗机开关就可以洗了.这就 ...
- Excel如何使用vlookup
一.vlookup的语法 VLOOKUP (lookup_value, table_array, col_index_num, [range_lookup]) ①Lookup_value为需要在数据表 ...
- iOS-启动项目(一)设置 rootViewController
摘要 刚创建一个新的项目,在 AppDelegate 中设置 rootViewController 来确定应用的首页是一个最基本的处理,因为是不常操作的处理,所以容易忽略其中的某个步骤,导致无法设置成 ...
- 转:addChildViewController实现网易新闻首页切换
本来只是打算介绍一下addChildViewController这个方法的,正好今天朋友去换工作面试问到网易新闻标签栏效果的实现,就结合它,用个小Demo实例介绍一下:(具体解释都写在了Demo里面的 ...
- 0RAYS元旦招新赛
一共有4道pwn题,题目不算难,但是挺考验调试能力的. pie 一个main函数就四次溢出... 第一次leak canary,第二次leak libc,第三次直接覆盖返回地址为one_gadgets ...
- Indirect函数(Excel函数集团)
此处文章均为本妖原创,供下载.学习.探讨! 文章下载源是Office365国内版1Driver,如有链接问题请联系我. 请勿用于商业!谢谢 下载地址:https://officecommunity-m ...
- C# 使用163的SMTP服务器发送邮件
string Receiver, string Subject, string content: //163邮箱发送配置 client = new System.Net.Mail.SmtpClient ...
- 如何获取网管MTU
在本机打开dos窗口,执行: ping -f -l 1472 192.168.0.1 其中192.168.0.1是网关IP地址,1472是数据包的长度.请注意,上面的参数是"-l" ...
- 使用xlsx实现Excel导入
需求 实现在系统里批量导入数据,通过上传一个excel文件,前端将文件处理为json数据发送给后端.(最好与后端定义好上传的文件模板,方便处理数据) 实现 使用xlsx: xlsx的github地址: ...
- SpringBoot整合zimg图片服务器
依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</arti ...