获取Map中的值

Map值如下

Map<String,Object> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");

1、在已知键值得情况下获取Map中的值

Object value1 = map.get("key1");
Object value2 = map.get("key2");
Object value3 = map.get("key3");

2、在未知键值的情况下获取Map的值

map.entrySet()
Set<Entry<String,Object>> entry = map.entrySet();
for(Entry<String,Object> value:entry){
     
	value.getValue();
}
map.keySet()
for(String key:map.keySet()){
     
	map.get(key);
}

你可能感兴趣的:(hashmap)