java如何遍历hashMap

通过Map的entrySet方法。将返回一个set集合。然后遍历这个set集合:

 

package com.howlaa.day04;

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

public class GenericTest {
	public static void main(String[] args) {
		HashMap<String,Integer> maps = new HashMap<String,Integer>();
		maps.put("zhang", 20);
		maps.put("hui", 22);
		Set<Map.Entry<String,Integer>> entrySet =  maps.entrySet();
		for (Map.Entry<String, Integer> entry : entrySet) {
			System.out.println(entry.getKey()+":"+entry.getValue());
		}
	}
	
}


 

 

posted on 2013-12-13 17:19  我的小人生  阅读(299)  评论(0编辑  收藏  举报