Updating Hashmap Values in Java
This article explains how to use the two methods contained in the HashMap class in Java - update put()
and replace()
update the values in a HashMap.
hashmap.put()
Update the value in Hashmap
in Java using
When we want to HashMap
insert a value in , we use put()
the method. We can also use it to update HashMap
the value in . In the following example, we create an HashMap
object of , which is composed of key-value pairs. When initializing, we need to define the data types of keys and values.
We use string types for both keys and values, and we can use the key to find or operate on the value. Below, we replace three
the value with key with a new value. If the HashMap
value we want to update does not exist in , using put()
the method, a new value will be inserted. 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("one", "Alex");
ourHashmap.put("two", "Nik");
ourHashmap.put("three", "Morse");
ourHashmap.put("four", "Luke");
System.out.println("Old Hashmap: " + ourHashmap);
ourHashmap.put("three", "Jake");
System.out.println("New Hashmap: " + ourHashmap);
}
}
Output:
Old Hashmap: {four=Luke, one=Alex, two=Nik, three=Morse}
New Hashmap: {four=Luke, one=Alex, two=Nik, three=Jake}
hashmap.replace()
Update the value in Hashmap
in Java using
HashMap
Another method of the class is replace()
that it can update or replace HashMap
existing values in . The biggest difference put()
between and is that when a key does not exist in , the method will insert the key and value into , but the method will return null. This makes it safer to use when updating values in .replace()
HashMap
put()
HashMap
replace()
replace()
HashMap
replace()
In the following example, we create a HashMap
and insert some key-value pairs. Then to update three
the value attached to the key , we use ourHashMap.replace(key, value)
, which takes two parameters, the first is the key we want to update and the second is the value.
import java.util.HashMap;
public class UpdateHashmap {
public static void main(String[] args) {
HashMap<String, String> ourHashmap = new HashMap<>();
ourHashmap.put("one", "Alex");
ourHashmap.put("two", "Nik");
ourHashmap.put("three", "Morse");
ourHashmap.put("four", "Luke");
System.out.println("Old Hashmap: " + ourHashmap);
ourHashmap.replace("three", "Jake");
System.out.println("New Hashmap: " + ourHashmap);
}
}
Output:
Old Hashmap: {four=Luke, one=Alex, two=Nik, three=Morse}
New Hashmap: {four=Luke, one=Alex, two=Nik, three=Jake}
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Difference between hashmap and map in Java
Publish Date:2025/04/14 Views:193 Category:Java
-
This tutorial explains the key differences between Map and in Java HashMap . In Java, Map is an interface for storing data in key-value pairs, and HashMap is Map an implementation class of the interface. Java has several classes ( TreeHashM
Get user home directory in Java
Publish Date:2025/04/14 Views:161 Category:Java
-
This tutorial explains how to get the user's home directory in Java and lists some sample code to guide you through the topic. For a multi-user operating system, each user has a file system directory; this directory is called the user's hom
Difference between size and length in Java
Publish Date:2025/04/14 Views:114 Category:Java
-
This tutorial explains the difference between size and length in Java. We have also listed some sample codes to help you understand the topic. Java has a size() method and a length property. Beginners may think that they are interchangeable
Mutex Locks in Java
Publish Date:2025/04/14 Views:198 Category:Java
-
In the field of computer science, mutual exclusion or mutex is known as the property of concurrency control. Every computer uses a minimum sequence of program instructions called a thread. At a time, the computer works on one thread. For be
How to compare characters for equality in Java
Publish Date:2025/04/14 Views:128 Category:Java
-
This tutorial shows you how to check if two characters are equal in Java. In Java, we can use the equals( == ) operator or the Character compare() equals() method of the Character class to compare two characters. If you are working with pri
Compile multiple Java files with a single command in Java
Publish Date:2025/04/14 Views:192 Category:Java
-
This tutorial explains how to compile multiple java files using a single command in Java. Compilation is a term used to refer to the process of converting java source code into bytecode using JDK. To execute any Java file, we need to follow
Arrow operator in Java ->
Publish Date:2025/04/14 Views:113 Category:Java
-
This tutorial explains - the role of the arrow operator ( ) in Java and lists some sample code to understand the topic. In Java 8, a new feature lambda expression was added, and the arrow operator appeared in Java to form lambda expressions
>> operator in Java
Publish Date:2025/04/14 Views:66 Category:Java
-
This guide will introduce you to the operator in Java . To understand this concept, you need to be familiar with some lower-level computing concepts. For example, bits, bytes, etc. Let's take a deeper look. Operators in Java In Java, the op
如何在 Java 中延迟几秒钟的时间
Publish Date:2023/12/17 Views:223 Category:Java
-
本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。