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.
First, we need to create a HashMap object. You can create an empty HashMap object through the following code:
HashMapmap = new HashMap ();
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:
HashMapmap = new HashMap ();
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);
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);
The above code will not change the key-value pair (key1, 1) because the key already exists in the HashMap.
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");
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.
You can use the for-each loop to traverse HashMap. The sample code is as follows:
for (Map.Entryentry : map.entrySet()) { String key = entry.getKey(); int value = entry.getValue(); System.out.println(key + " -> " + value); }
The above code traverses all key-value pairs in HashMap , and print their keys and values.
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");
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!