IT源码网

map中的值更新

flyfish 2021年04月03日 程序员 664 0
map中存的是key->value
如果要更新map中存的某对值,直接map.put("","");即可;
执行后,map中与key值匹配的那对值将会被更新


代码:
public class updateMap {
public static void main(String args[]){
Map<String ,String > map = new HashMap<String,String>();
map.put("紫光","华宇");
map.put("亿信","华辰");

Set<Map.Entry<String,String>> set = map.entrySet();

Iterator<Map.Entry<String,String>> _iter = set.iterator();

while(_iter.hasNext()){
Map.Entry<String,String> entry = _iter.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}

结果:
紫光:华宇
亿信:华辰

代码:
public class updateMap {
public static void main(String args[]){
Map<String ,String > map = new HashMap<String,String>();
map.put("紫光","华宇");
map.put("紫光","华辰");

Set<Map.Entry<String,String>> set = map.entrySet();

Iterator<Map.Entry<String,String>> _iter = set.iterator();

while(_iter.hasNext()){
Map.Entry<String,String> entry = _iter.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}结果:
紫光:华辰
评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!