[Java] Understand in 10 minutes! Associative array and HashMap

Introduction

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.

What is an associative array?

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.

連想配列.png

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

[Java] Understand in 10 minutes! Associative array and HashMap
Initializing HashMap in Java
Java classes and instances to understand in the figure
Gzip-compress byte array in Java and output to file
Java Stream API in 5 minutes
Make your own simple server in Java and understand HTTP
[Java] Difference between Hashmap and HashTable
Encoding and Decoding example in Java
[Java] Declare and initialize an array
[Java] Difference between array and ArrayList
Understanding equals and hashCode in Java
Hello world in Java and Gradle
[Java] array
When seeking multiple in a Java array
Java array
Java reference to understand in the figure
java (array)
Understand java interface in your own way
Map without using an array in java
Java array
[Java] Array
[Java] for Each and sorted in Lambda
Organized memo in the head (Java --Array)
Java array
Arrylist and linked list difference in java
Program PDF headers and footers in Java
Understand the difference between int and Integer and BigInteger in java and float and double
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Understand in 5 minutes !! How to use Docker
java array
[Java] Array
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
Discrimination of Enums in Java 7 and above
HashMap # putAll () behaves differently between Java 7 and Java 8
How to ZIP a JAVA CSV file and manage it in a Byte array
[Java] Understand the difference between List and Set
Regarding the transient modifier and serialization in Java
Create barcodes and QR codes in Java PDF
Detect similar videos in Java and OpenCV rev.2
Deep dive into how HashMap works in Java
Parallel and parallel processing in various languages (Java edition)
Difference between next () and nextLine () in Java Scanner
[Swift vs Java] Let's understand static and final
Differences in writing Java, C # and Javascript classes
Capture and save from selenium installation in Java
Detect similar videos in Java and OpenCV rev.3
Add, read, and delete Excel comments in Java
Check static and public behavior in Java methods
Basics of threads and Callable in Java [Beginner]
Distinguish between positive and negative numbers in Java
Java adds and removes watermarks in word documents
Detect similar videos in Java and OpenCV rev.1
Represents "next day" and "previous day" in Java / Android
Questions in java exception handling throw and try-catch
Upload and download notes in java on S3
Encrypt / decrypt with AES256 in PHP and Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
java array variable