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

I created a hash map in which users enter the key and value. I want to be able to change the value of the hash map if a certain key is entered. I tried the setValue method but received nothing. The value and key are strings. What method would I use to change this?

+5
source share
1 answer

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

Conclusion:

 {user=Luiggi Mendoza} {user=Oli Charlesworth} 
+25
source

Source: https://habr.com/ru/post/1200165/


All Articles