How To Change A Value In A Hashmap Java
Update Value in Hashmap in Java
- Update Value in Hashmap Using
hashmap.put()
in Java - Update Value in Hashmap Using
hashmap.supplant()
in Coffee
This article introduces how to update a value in a HashMap in Java using 2 methods - put()
and supersede()
included in the HashMap form.
Update Value in Hashmap Using hashmap.put()
in Java
We use the put()
method with HashMap
when we desire to insert a value into the HashMap
. And we can also utilise it to update the value inside the HashMap
. In the example below, we create an object of HashMap
, which is made up of key-value pairs and it is required to define the data blazon of both the key and the value during the initialization.
We use the cord type for both key and value and we can find or do operations on the value using the fundamental. Below, nosotros supersede the value that has the cardinal three
with a new value. If at that place is no existing presence in the HashMap
that nosotros want to update and utilize the put()
method, it will insert a new value. The output shows the updated value.
import java.util.HashMap; public class UpdateHashmap { public static void main(String[] args) { HashMap<String, String> ourHashmap = new HashMap<>(); ourHashmap.put("ane", "Alex"); ourHashmap.put("ii", "Nik"); ourHashmap.put("three", "Morse"); ourHashmap.put("four", "Luke"); System.out.println("Old Hashmap: "+ourHashmap); ourHashmap.put("three", "Jake"); Organisation.out.println("New Hashmap: "+ourHashmap); } }
Output:
Old Hashmap: {4=Luke, one=Alex, 2=Nik, three=Morse} New Hashmap: {four=Luke, one=Alex, two=Nik, 3=Jake}
Update Value in Hashmap Using hashmap.supplant()
in Java
Another method that comes with the HashMap
class is replace()
that can update or replace an existing value in a HashMap
. The big deviation between put()
and replace()
is that when a key does not exist in the HashMap
, the put()
method inserts that key and the value inside the HashMap
, merely the supplant()
method will return null. This makes replace()
safer to use when updating a value in a HashMap
.
In the following example, nosotros create a HashMap
and insert some key-value pairs. Then to update the value attached to the key iii
, we utilise ourHashMap.supersede(key, value)
that takes two arguments, the commencement key that nosotros desire to update and the second the value.
import coffee.util.HashMap; public class UpdateHashmap { public static void principal(Cord[] args) { HashMap<String, String> ourHashmap = new HashMap<>(); ourHashmap.put("one", "Alex"); ourHashmap.put("two", "Nik"); ourHashmap.put("3", "Morse"); ourHashmap.put("four", "Luke"); System.out.println("Old Hashmap: "+ourHashmap); ourHashmap.replace("3", "Jake"); Arrangement.out.println("New Hashmap: "+ourHashmap); } }
Output:
Old Hashmap: {four=Luke, one=Alex, two=Nik, three=Morse} New Hashmap: {iv=Luke, i=Alex, 2=Nik, three=Jake}
Write for usa
DelftStack manufactures are written by software geeks like yous. If you also would like to contribute to DelftStack by writing paid articles, y'all tin can check the write for us page.
Related Article - Java HashMap
How To Change A Value In A Hashmap Java,
Source: https://www.delftstack.com/howto/java/update-value-in-hashmap-java/
Posted by: cooksidid1965.blogspot.com
0 Response to "How To Change A Value In A Hashmap Java"
Post a Comment