我已经创建了一个哈希表,让用户输入键和值。如果输入特定的键,我想能够更改哈希表的值。我尝试使用setValue
方法,但没有效果。值和键都是字符串。我该使用什么方法来更改它?
只需使用Map#put
方法,将当前旧键和新值作为参数传入:
Map<String, String> map = new HashMap<>();
map.put("user", "Luiggi Mendoza");
System.out.println(map);
//replacing the old value
map.put("user", "Oli Charlesworth");
System.out.println(map);
输出:
{user=Luiggi Mendoza}
{user=Oli Charlesworth}
String newprice2 = String.valueOf(newprice);
cart.put(item, newprice2);
} - pgray10item
变量的类提供equals
和hashCode
实现。 - Luiggi Mendoza
put
一个新值即可。 - Oliver Charlesworth