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. 2021 中国.NET开发者峰会近50场热点技术专题揭秘

    01 大会介绍  .NET Conf China 2021 是面向开发人员的社区峰会,基于 .NET Conf 2021的活动,庆祝 .NET 6 的发布和回顾过去一年来 .NET 在中国的发展成果展 ...

  2. hitcon_2018_children_tcache(off by null)

    拿到题目例行检查 (我就不放了) 将程序放入ida中 很明显的堆的题目,然后我们进入add函数 可以看到将s复制到dest里面,说明存在off by null 漏洞 这道题目我也上网查询了师傅们的wp ...

  3. [BUUCTF]REVERSE——SimpleRev

    SimpleRev 附件 步骤: 例行查壳儿,,无壳,64位程序 64位ida载入,看main函数 关键代码段在Decry函数里 unsigned __int64 Decry() { char v1; ...

  4. Landsat 现有 Analysis Ready Data (ARD) 数据介绍

    Global Web-Enabled Landsat Data (GWELD)[1] NASA 原先的 Web-Enabled Landsat Data Conterminous U.S. Seaso ...

  5. Python的 垃圾回收机制

    垃圾回收 1. 小整数对象池 整数在程序中的使用非常广泛,Python为了优化速度,使用了小整数对象池, 避免为整数频繁申请和销毁内存空间. Python 对小整数的定义是 [-5, 257) 这些整 ...

  6. JavaScript 判断变量是否为空---三元运算,元组获取值-重组.map()。

    var from_days = getQueryString('from_days'); undefined {'from_days': (from_days) ? from_days : null} ...

  7. Could not synchronize database state with session问题,说保存空

    Could not synchronize database state with session问题,说保存空 ,可以在post.hbm.xml文件里设置inverse="true&quo ...

  8. MAVEN项目打包报错:Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on pr ...

  9. UE4之第一个飞机游戏

    开始之前 UE4官网 初识ue4教程(1~9节): https://www.bilibili.com/video/BV164411Y732?p=1 第一个飞机游戏: http://www.sikied ...

  10. 定义Anroid SO崩溃位置

    E:\android-ndk-r13b\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin> arm-linux-a ...