遍历Map

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class MapTest {
	
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("wo", "melon");
		map.put("ni", new String[]{"是","不","是","白","天"});
		
		for(Map.Entry<String, Object> entry : map.entrySet()){
			String key = entry.getKey();
			System.out.println("key: "+"----------"+key);
			Object value = entry.getValue();
			if(value instanceof String[]){
				System.out.println(Arrays.toString((String[])value));
			}else{
				System.out.println("value: "+"----------"+value);
			}
		}
	}
}

结果:

key: ----------wo
value: ----------melon
key: ----------ni
[是, 不, 是, 白, 天]


你可能感兴趣的:(遍历Map)