---
title: "Java.在一个循环，同时输出Map的key和value"
author: "8850702"
date: "2016-03-12T18:20:06Z"
tags: []
categories: ["移动开发"]
slug: "java-map-key-value"
draft: false
lastmod: "2016-03-12T18:20:06Z"
id: 2282399
url: "https://8850702.iteye.com/blog/2282399"
---

```java

//在一个循环，同时输出key和value
	public static void main(String[] args) {
		Map<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("a", "1234");
		hashMap.put("m", "oi");
		hashMap.put("p", "看");
		hashMap.put("d", "fuck");
		hashMap.put("e", "2");
		hashMap.put("v", "-_-");//以上，添加元素

		//将Map接口，变为Set接口
		Set<Entry<String, String>> entrySet = hashMap.entrySet();
		//得到Iterator
		Iterator<Entry<String, String>> iterator = entrySet.iterator();
		
		//同时输出key和value
		while (iterator.hasNext()) {
			//通过iterator.next()，得到key和value
			Entry<String, String> next = iterator.next();
			System.out.println(next.getKey() + " -> " + next.getValue());
		}

```