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种方法的更多相关文章

  1. 遍历hashmap 的四种方法

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

  2. 遍历HashMap的四种方法

    public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...

  3. 转载:遍历Map的四种方法

    http://www.cnblogs.com/kristain/articles/2033566.html 遍历Map的四种方法 public static void main(String[] ar ...

  4. java 遍历map的四种方法

    16:21:42 Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是Map中的一个接口,他的用途是表示一个映射项( ...

  5. 遍历python字典几种方法

    遍历python字典几种方法 from: http://ghostfromheaven.iteye.com/blog/1549441 aDict = {'key1':'value1', 'key2': ...

  6. Java遍历集合的几种方法

    遍历集合的几种方法 用不同的方法遍历集合. public interface Iterator:对Collection进行迭代的迭代器.迭代器取代了Java Collections FrameWork ...

  7. 遍历map的四种方法

    方法一  在for-each循环中使用entries来遍历这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用.注意:for-each循环在Java 5中被引入所以该方法只能应用于j ...

  8. java中遍历map的几种方法介绍

          喜欢用Java写程序的朋友都知道,我们常用的一种数据结构map中存储的是键值对,我们一般存储的方式是: map.put(key, value); 而提取相应键的值用的方法是: map.ge ...

  9. java 遍历Map的4种方法

    在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...

随机推荐

  1. 使用plantuml,业务交接就是这么简单

    使用plantuml,业务交接就是这么简单 你好,我是轩脉刃. 最近交接了一个业务,原本还是有挺复杂的业务逻辑的,但发现交接过来的项目大有文章,在项目代码中有一个docs文件夹,里面躺着若干个 pum ...

  2. spring security oauth2搭建resource-server demo及token改造成JWT令牌

    我们在上文讲了如何在spring security的环境中搭建基于oauth2协议的认证中心demo:https://www.cnblogs.com/process-h/p/15688971.html ...

  3. c#中Array,ArrayList 与List<T>的区别、共性与转换

    本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...

  4. AT4151 [ABC099B] Stone Monument 题解

    Content 一个村里有 \(999\) 个房子,第 \(i\) 个房子的高度为 \(1+2+...+i=\sum\limits_{j=1}^ij\).现在下了一场雪,给定相邻两个房子没被雪覆盖的高 ...

  5. python 字符编码讲解

    ANSI不是一种具体的编码格式 ANSI在中文Windows操作系统代码指的是GBK编码 ANSI在中文Mac操作系统代码指的是UTF-8编码 ANSI在其他国家的操作系统中有其他的编码格式 #ASC ...

  6. HTTPS握手-混合加解密过程

    SSL协议通信过程 (1) 浏览器发送一个连接请求给服务器;服务器将自己的证书(包含服务器公钥S_PuKey).对称加密算法种类及其他相关信息返回客户端; (2) 客户端浏览器检查服务器传送到CA证书 ...

  7. 【LeetCode】242. Valid Anagram 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...

  8. 【LeetCode】859. Buddy Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  9. 1188 最大公约数之和 V2

    1188 最大公约数之和 V2 题目来源: UVA 基准时间限制:2 秒 空间限制:262144 KB  给出一个数N,输出小于等于N的所有数,两两之间的最大公约数之和.       相当于计算这段程 ...

  10. B. Recover the String

    B. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...