How can I change the value of a key in a hash map?

61,801

Just use Map#put using the current old key and the new value:

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);

Output:

{user=Luiggi Mendoza}
{user=Oli Charlesworth}
Share:
61,801
pgray10
Author by

pgray10

Updated on June 27, 2020

Comments

  • pgray10
    pgray10 almost 4 years

    I have created a hash map that the users input the key and the value. I want to be able to change the value of the hash map if a specific key is entered. I tried the setValue method but got nothing. The value and the key are both strings. What method would I use to change this?