---
title: "Map的几种循环方式"
author: "chengjie177"
date: "2011-08-25T13:39:12Z"
tags: []
categories: ["编程语言"]
slug: "map"
draft: false
lastmod: "2011-08-25T13:39:12Z"
id: 1157786
url: "https://wocece.iteye.com/blog/1157786"
---

根据JDK的新特性,用For循环Map,例如循环Map的Key

```java
for(String dataKey : paraMap.keySet())   
{   
    System.out.println(dataKey );              
}
```
  
这里要注意的是,paraMap是怎么样定义的,如果是简单的Map paraMap = new HashMap();那前面的String就只能换成Object了.
对整Map的key和value都进行循环,如下:


```java
for(Map.Entry<String, Object> entry : paraMap.entrySet())   
{   
    System.out.println(entry.getKey()+": "+entry.getValue());   
}
```
  
要是在以前,则是这么循环的:
java 代码

```java
 Iterator it = paraMap.entrySet().iterator();   
while (it.hasNext())    
{   
        Map.Entry pairs = (Map.Entry)it.next();   
        System.out.println(pairs.getKey() + " = " + pairs.getValue());   
 } 
```