Home> Java> javaTutorial> body text

How to use HashMap function for mapping operations in Java

WBOY
Release: 2023-06-26 16:38:24
Original
1272 people have browsed it

The HashMap function is a very commonly used mapping function in Java, which allows us to store and access data in the form of key-value pairs. This article will introduce how to use the HashMap function for mapping operations.

First of all, we need to understand what the HashMap function is. HashMap is a hash table data structure in Java that implements the Map interface. It allows us to store key-value pairs and retrieve the corresponding value by key. HashMap converts the key into an index through a hash function and stores the value at the location corresponding to the index. When we need to get a value, we use the key to calculate the index where the value is located, and find the corresponding value at the position of the index.

Next, let’s take a look at the specific usage of the HashMap function.

  1. Create a HashMap object

First, we need to create a HashMap object. You can create an empty HashMap object through the following code:

HashMap map = new HashMap();
Copy after login

where K and V represent the key and value types respectively. For example, if we want to store string type keys and integer type values, we can write:

HashMap map = new HashMap();
Copy after login
  1. Add key-value pairs

Next, we can use put Method to add key-value pairs to HashMap, the sample code is as follows:

map.put("key1", 1); map.put("key2", 2); map.put("key3", 3);
Copy after login

This code will add three key-value pairs to HashMap, namely (key1, 1), (key2, 2) and (key3 , 3).

It is worth noting that if the key we add already exists in the HashMap, the put method will overwrite the value corresponding to the key. If we don't want to replace the existing key-value pair, we can use the putIfAbsent method. This method will only add a key-value pair when the key does not exist. The sample code is as follows:

map.putIfAbsent("key1", 4);
Copy after login

The above code will not change the key-value pair (key1, 1) because the key already exists in the HashMap.

  1. Get the key-value pair

Next, we can use the get method to get the value corresponding to the key from the HashMap. The sample code is as follows:

int value = map.get("key1");
Copy after login

This code will get the value with key "key1" and assign it to the value variable.

It should be noted that if we get the value of a key that does not exist, null will be returned.

  1. Traverse HashMap

You can use the for-each loop to traverse HashMap. The sample code is as follows:

for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); int value = entry.getValue(); System.out.println(key + " -> " + value); }
Copy after login

The above code traverses all key-value pairs in HashMap , and print their keys and values.

  1. Delete key-value pairs

Finally, we can use the remove method to delete the key-value pairs in the HashMap. The sample code is as follows:

map.remove("key1");
Copy after login

This code The key-value pair with key "key1" will be deleted. If the key does not exist, the remove method will have no effect.

Through the above steps, we can use the HashMap function to perform mapping operations. It is important to note that when using a HashMap, the keys need to be unique and immutable. Therefore, we need to make sure that the key type we use is an immutable type, such as a string, integer, or enumeration type.

The above is the detailed content of How to use HashMap function for mapping operations in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!