java中遍历map的几种方法介绍
喜欢用Java写程序的朋友都知道,我们常用的一种数据结构map中存储的是键值对,我们一般存储的方式是:
map.put(key, value);
而提取相应键的值用的方法是:
map.get(key);
确实,这些就是map的基本方法,可是,如果我想查看map中所有的元素怎么办?也就是想遍历其中的每个元素。
下面,我就介绍三种方法遍历map:
说明:我在map中存储的值是CSDN一博主的QQ签名,当我遍历的时候你会看到。
1.map不是集合,所以,map并不存在迭代器,但是,map中的一个方法可以使得map中的键成为一个集合,那么我们就可以利用集合的迭代器进行遍历,方法如下所示:
Set<Integer> s = (Set<Integer>)map.keySet();
Iterator<Integer> it = s.iterator();
int Key;
String value;
while(it.hasNext()) {
Key = it.next();
value = (String)map.get(Key);
System.out.println(Key+":\t"+value);
}
2.map提供了另一个方法--entryset,使得我们可以获得map中存储的“键值对”集合,我们可以对键值对集合进行操作,进行遍历,方法如下所示:
Set<Entry<Integer, String>> s = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = s.iterator();
Map.Entry<Integer, String> entry;
int Key;
String value;
while(it.hasNext()) {
entry = it.next();
Key = entry.getKey();
value = entry.getValue();
System.out.println(Key+":\t"+value);
}
3.其实这种方法和第二种方法是一样的,只是我们用了不同的形式实现而已,这里不多说,方法如下:
for(Map.Entry<Integer, String> entry : map.entrySet()){
System.out.println(entry.getKey() + ":\t" + entry.getValue());
}
下面给出完整的测试代码以及运行后的结果:
package com.brucezhang.test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapItTest {
private static Map<Integer, String> my_map = new HashMap<Integer, String>();
private static String[] my_value = {"I love three things", "The sun for the day", "The moon for the night", "The you forever"};
/**
* @param args
* Author:DLUTBruceZhang
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//给 map 赋初值
for (int i = 0; i < my_value.length; i++) {
my_map.put(i, my_value[i]);
}
//使用第一种方法进行遍历
theFirstItFun(my_map);
System.out.println("----------------------------------------");
//使用第二种方法进行遍历
theSecondItFun(my_map);
System.out.println("----------------------------------------");
//使用第三种方法进行遍历
theThirdItFun(my_map);
}
public static void theFirstItFun(Map<Integer, String> map) {
Set<Integer> s = (Set<Integer>)map.keySet();
Iterator<Integer> it = s.iterator();
int Key;
String value;
while(it.hasNext()) {
Key = it.next();
value = (String)map.get(Key);
System.out.println(Key+":\t"+value);
}
}
public static void theSecondItFun(Map<Integer, String> map){
Set<Entry<Integer, String>> s = map.entrySet();
Iterator<Map.Entry<Integer, String>> it = s.iterator();
Map.Entry<Integer, String> entry;
int Key;
String value;
while(it.hasNext()) {
entry = it.next();
Key = entry.getKey();
value = entry.getValue();
System.out.println(Key+":\t"+value);
}
}
public static void theThirdItFun(Map<Integer, String> map){
for(Map.Entry<Integer, String> entry : map.entrySet()){
System.out.println(entry.getKey() + ":\t" + entry.getValue());
}
}
}
运行结果如下:
0: I love three things 1: The sun for the day 2: The moon for the night 3: The you forever ---------------------------------------- 0: I love three things 1: The sun for the day 2: The moon for the night 3: The you forever ---------------------------------------- 0: I love three things 1: The sun for the day 2: The moon for the night 3: The you forever
java中遍历map的几种方法介绍的更多相关文章
- Java中遍历map的四种方法 - 转载
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都 ...
- Java中遍历Map的几种方法
转自: http://blog.csdn.net/wzb56/article/details/7864911 方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基 ...
- 谈谈java中遍历Map的几种方法
java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点 先初始化一个map public ...
- java 中遍历Map的几种方法
方法分为两类: 一类是基于map的Entry:map.entrySet(); 一类是基于map的key:map.keySet() 而每一类都有两种遍历方式: a.利用迭代器 iterator: b.利 ...
- java中遍历map对象的多种方法
在Java中如何遍历Map对象 How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...
- Java原来如此-遍历Map的三种方法
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; pub ...
- Java中遍历Map的四种方式
Demo如下 Map<String, String> map = new HashMap<>(); map.put("key1","data1&q ...
- java中遍历map的两种方式
1.先将map对象转成set,然后再转为迭代器 Iterator iterator = map.entrySet().iterator(); while(iterator.hasNext()){ En ...
- java中遍历MAP,嵌套map的几种方法
java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>(); map.put("us ...
随机推荐
- 【HDU 2669】Romantic
Problem Description The Sky is Sprite.The Birds is Fly in the Sky.The Wind is Wonderful.Blew Throw t ...
- 【LSGDOJ1836】: 量化交易 贪心
题目描述 applepi 训练了一个可以自动在股票市场进行量化交易的模型.通常来说,applepi 写出的模型,你懂得,就好比一架印钞机.不过为了谨慎起见,applepi还是想先检查一下模型的效果.a ...
- 【bzoj4572 scoi2016】围棋
题目描述 近日,谷歌研发的围棋AI—AlphaGo以4:1的比分战胜了曾经的世界冠军李世石,这是人工智能领域的又一里程碑. 与传统的搜索式AI不同,AlphaGo使用了最近十分流行的卷积神经网络模型. ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- bzoj3437小P的牧场 斜率优化dp
3437: 小P的牧场 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1542 Solved: 849[Submit][Status][Discus ...
- 在右键中添加以管理员运行CMD命令提示符 (进化版)
直接代码,转过来的 20180316更新添加快捷键A,点右键按A即可: Windows Registry Editor Version 5.00 ; Created by: Shawn Brink ; ...
- IDEA 整合 SSM 框架学习
认识 Spring 框架 更多详情请点击这里:这里 Spring 框架是 Java 应用最广的框架,它的成功来源于理念,而不是技术本身,它的理念包括 IoC (Inversion of Control ...
- c++中成员函数的参数名与成员变量名重合的问题
有一天写类的时候突然想到了这个问题,下面就来介绍如何解决这个问题. 定义一个类: class test{ public: void setnum(); void getnum(); private: ...
- jQuery ajax中使用serialize()方法提交表单数据示例
<form id="form"> 输入账号 :<input id="name" type="text" name=&quo ...
- 转:Kafka 客户端TimeoutException问题之坑
原文出自:http://www.jianshu.com/p/2db7abddb9e6 各种TimeoutException问题 会抛出org.apache.kafka.common.errors.Ti ...