java:将hashmap值更改为另一个值
我有一张这样的地图java:将hashmap值更改为另一个值,java,map,hashmap,Java,Map,Hashmap,我有一张这样的地图 HashMap<String, Integer> map = new HashMap<String, Integer>(); map.put("sun",0); map.put("Sunday",0); map.put("sund",0); map.put("Mon", 1); map.put("Tues", 2); map.put("Wed", 3); 我该怎么做?? 谢谢你试试这个 for(Entry<String, Integer
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("sun",0);
map.put("Sunday",0);
map.put("sund",0);
map.put("Mon", 1);
map.put("Tues", 2);
map.put("Wed", 3);
我该怎么做??
谢谢你试试这个
for(Entry<String, Integer> e : map.entrySet()) {
if (e.getValue() == 0) {
e.setValue(0);
}
}
for(条目e:map.entrySet()){
如果(如getValue()==0){
e、 设定值(0);
}
}
Set keyValues=map.keySet();
用于(字符串s:keyValues){
int i=map.get(s);
如果(i==0)
{
地图.put(s,7);;
}
}
使用此代码,可能会帮助您:
if (hashMap1.containsKey(key))
{
valuesCopy = hashMap1.get(key); // first, copy out the existing values
valuesCopy.add(newValues++); // insert the newValues to the value Set
hashMap1.put(key, valuesCopy); // insert the key-value pairs
}
您可以通过以下方式完成此操作:
for(Map.Entry<String, Integer> e: map.entrySet()){
if(0 == (e.getValue())) {
e.setValue(7);
}
}
for(Map.Entry e:Map.entrySet()){
如果(0==(e.getValue()){
e、 设定值(7);
}
}
for(条目:map.entrySet()){
如果(如getValue()==0){
e、 设定值(7);
}
}
它应该能用。试试这个
礼节:
HashMap方法来更改HashMap中的内容
Object objectReplacedForKey =
hashMapName.put(objectKey, objectToAdd);
hashMapName.putAll(mapToAdd);
hashMapName.remove(keyObject);
hashMapName.clear();
迭代、检查、更改。如果您不确定,可以搜索如何遍历地图
for(Map.Entry<String, Integer> e: map.entrySet()){
if(0 == (e.getValue())) {
e.setValue(7);
}
}
for(Entry<String,Integer> entry : map.entrySet()) {
if(e.getValue() == 0) {
e.setValue(7);
}
}
Object objectReplacedForKey =
hashMapName.put(objectKey, objectToAdd);
hashMapName.putAll(mapToAdd);
hashMapName.remove(keyObject);
hashMapName.clear();