Java Map 遍历

Map<String, Object> map = new HashMap<String,Object>();
		map.put("cn", "中国");
		
		//for-each entrySet
		for(Map.Entry<String, Object> entry : map.entrySet()){
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
		
		//for-each keySet
		for(String key : map.keySet()){
			System.out.println(key);
			System.out.println(map.get(key));
		}
		
		//iterator entrySet  while 
		Set<Map.Entry<String,Object>> set = map.entrySet();
		Iterator<Map.Entry<String,Object>> iter = set.iterator();
		while(iter.hasNext()){
			Map.Entry<String,Object> entry = iter.next();
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
		
		//iterator keySet  while
		Set<String> keySet = map.keySet();
		Iterator<String> iterKey = keySet.iterator();
		while(iterKey.hasNext()){
			String key = iterKey.next();
			System.out.println(key);
			System.out.println(map.get(key));
		}

 

补充:interface Entry是Map的一个内部接口,所以使用时都是 Map.Entry 的形式。

Java Map 遍历
 

你可能感兴趣的:(java,map)