Two kinds of traversal Java / Map of

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Bill_Marz/article/details/102769850

Interface Map <K, V>
maps the key value to the object. A map can not contain duplicate keys; each key can only be mapped to a value at most.

A first traverse
the map keySet () method to get all keys key component, and for enhancing loop through the key, the return value of the specified key is mapped by map.get (key);


	Map<String,String> map  = new HashMap<String,String>();
	map.put("XTF", "XBZ");
	map.put("CGX", "ZXT");
	map.put("LYP", "WF");
	
	
	Set<String> keys = map.keySet();
	for (String key : keys) {
		//String value = map.get(key);
		System.out.println(key+" "+map.get(key));
	}
				
	

The second traverse
with Map.Entry () method comprising obtaining a set of mappings Set entrys, for reinforcement and then loop through entrys, through entry object entry.getKey () and entry.getValue () Gets K, V


	Map<String,String> map = new HashMap<String,String>();
	map.put("XTF", "XBZ");
	map.put("CGX", "ZXT");
	map.put("LYP", "WF");
	
	Set<Map.Entry<String,String>> entrys = map.entrySet();
	for (Entry<String, String> entry : entrys) {
		String key = entry.getKey();
		String value = entry.getValue();
		System.out.println("Key:"+key+"  "+"value:"+value);
	}
	

Guess you like

Origin blog.csdn.net/Bill_Marz/article/details/102769850