遍历Map的几种方法

Map遍历方式

//LinkedHashMap也是一个HashMap,但是内部维持了一个双向链表,可以保持顺序
		Map<String, String> maps = new LinkedHashMap<String, String>();
		maps.put("1", "aaaa");
		maps.put("2", "bbb");
		maps.put("3", "ccc");
		maps.put("4", "ddd");
//用JDK5中增强for循环的方式
for(Map.Entry<String, String> map: maps.entrySet()) {
	System.out.println("key: " + map.getKey() + "-- value: " + map.getValue());
}


//用keySet
for(String str: maps.keySet()) {
	System.out.println("key; " + str + "-- value:" + maps.get(str));
}


//Iterator方式
for(Iterator i=maps.entrySet().iterator(); i.hasNext(); ) {
	Map.Entry<String, String> me = (Map.Entry<String, String>)i.next();
	System.out.println("key:" + me.getKey() + "value:" + me.getValue());
}









   

你可能感兴趣的:(jdk,String,HashMap,iterator)