You can also use associative arrays in Java. What is an associative array in the first place? In addition to solving the question, I think that you will be able to understand how to use HashMap, which will be used in Java, in about 10 minutes.
The question is, what is an associative array? As you probably know arrays in Java, you can retrieve them by array name [index number]. On the other hand, unlike a database, it is not possible to store values or retrieve specified elements based on an image stored in an array. For example, even if the price of "apple" is "100 yen", there is an inconvenience that you cannot freely put in and take out the price by using words as landmarks.
The solution to this is the story of the "associative array" that I will explain. In the previous example, "apple" is the key, and the associated "100 yen" is the value. As you can see in the image example below, an array in which words are connected by key and value is called an "associative array". It is an image that you can get "100 yen" by specifying "apple" in the array.
Using an associative array can be achieved by using the HashMap described below.
HashMap
In order to use HashMap, you need to create an object of HashMap class. Like this.
HashMapTest.java
HashMap hmap<String,Integer> = new HashMap<String,Integer>();
When you create an object of HashMap class, you can use methods such as mapping and deleting key and value on the map. The main methods are listed below.
Method name | argument | Description |
---|---|---|
put | First argument: K key,Second argument: V value | Map the key and the value associated with the key to the map. |
get | First argument: Object key | By passing the key as the first argument, the value associated with the key is returned as the return value. |
remove | First argument: Object key | If there is a mapping of the specified key, it will be removed from the map. |
replace | First argument: K key,Second argument: V value | Replaces value with the second argument only if the specified key exists and some value is set. |
containsKey | First argument: Object key | Returns true in the return value if the map contains a mapping for the specified key. |
containsValue | First argument: Object value | Returns true in the return value if the map contains a mapping of the specified value. |
clear | No arguments | Remove all mappings from the map. |
isEmpty | No arguments | Returns true if the map does not retain a key-value mapping. |
size | No arguments | Returns the number of key value mappings in the map as a return value. |
We have also prepared samples as follows, so we would appreciate it if you could refer to "Hmm. Use it like this."
HashMapTest.java
import java.util.HashMap;
public class HashMapTest {
public static void main(String[] args) {
//TODO auto-generated method stub
HashMap<String, Integer> hmap = new HashMap<String, Integer>();
//add to
hmap.put("Apple", 100);
hmap.put("banana", 200);
hmap.put("Grape", 300);
//Get
System.out.println(hmap.get("Apple"));
//Does the specified key be included?(Returns true because it is included)
System.out.println("containskey:" + hmap.containsKey("banana"));
//Does the specified value be included (false is returned because it is not included)
System.out.println("containsValue:" + hmap.containsValue(400));
//Delete
hmap.remove("Grape");
//Get the number of mappings
System.out.println("size:" + hmap.size());
//Delete all
hmap.clear();
System.out.println(hmap.isEmpty());
}
}
What did you think. Please use HashMap for business and learning. Let's do it.
Recommended Posts