码迷,mamicode.com
首页 > 其他好文 > 详细

遍历map

时间:2015-01-07 12:59:01      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

map

package l6.e5.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class MapTest5My2
{
	public static void main(String[] args)
	{
		HashMap<String,String> map = new HashMap<String,String>();
		
		map.put("a", "aa");
		map.put("b", "bb");
		map.put("c", "cc");
		map.put("d", "dd");
		
		Set<String> set = map.keySet();
		//keyset 1 
		for (String key : set) {
			System.out.println("key= "+ key + " and value= " + map.get(key));
		}
		//keyset 2
		Iterator<String> it=set.iterator();
		while (it.hasNext()) {
			String key = (String) it.next();
			String value=map.get(key);
			System.out.println(key+":"+value);
			
		}
		
		//entryset
		Set<Map.Entry<String, String>> set2= map.entrySet();
		
		for (Map.Entry<String, String> entry : set2) {
			System.out.println("key :" + entry.getKey() + " and value :" + entry.getValue());
		}
		
		
		
	}
}

  

遍历map

标签:

原文地址:http://www.cnblogs.com/stit/p/4208089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!