[Java] How to use Map

Programming study diary

January 5, 2021 A brief summary of how to handle Maps in Java.

What is Map?

Map can store multiple data consisting of two elements, key and value. A key is like giving a name to a value, and each value has a key. The feature of Map is that this key and value are paired. Therefore, when searching for a value from Map, you can search for the key as a marker.

How to initialize with HashMap

Map is an interface, so you have to use a class that implements it to instantiate it. The HashMap class is often used to instantiate a Map.

How to declare using the HashMap class


Map<Key model name,Value type name>Object name= new HashMap<>();

You must specify the key and value data types when declaring.

Sample code


import java.util.HashMap;
import java.util.Map;

public class Sample {
    public static void main(String[] args) throws Exception {
        //When the key is Integer and the value is String
        Map<Integer, String> map = new HashMap<>();
    }
}

Frequently used methods in Map

put method

A method that can add data to Map, specify the key in the first argument and the value in the second argument.

Sample code


import java.util.HashMap;
import java.util.Map;

public class Sample {
           public static void main(String[] args) throws Exception {
                      Map<Integer, String> map = new HashMap<>();
                      map.put(1, "Tanaka");
                      map.put(3, "Suzuki");
                      map.put(5, "Yamada");
           }
}

get method

It is a method that can get the data stored in Map, and you can get the value of the key by specifying the key in the argument.

Sample code


import java.util.HashMap;
import java.util.Map;

public class Sample {
           public static void main(String[] args) throws Exception {
                      Map<Integer, String> map = new HashMap<>();
                      map.put(1, "Tanaka");
                      map.put(3, "Suzuki");
                      map.put(5, "Yamada");

                      System.out.println(map.get(1));
                      System.out.println(map.get(3));
                      System.out.println(map.get(5));
           }
}

Execution result


Tanaka
Suzuki
Yamada

keySet method

A method that returns the value of the key contained in the Map. This time, I will introduce how to output all the keys of Map using the keySet method.

Sample code


import java.util.HashMap;
import java.util.Map;

public class Sample {
           public static void main(String[] args) throws Exception {
                      Map<Integer, String> map = new HashMap<>();
                      map.put(1, "Tanaka");
                      map.put(3, "Suzuki");
                      map.put(5, "Yamada");

                      for(Integer key:map.keySet()) {
                                 System.out.println(key);
                      }
           }
}

Execution result


1
3
5

values ​​method

The values ​​method can receive the defined Map and enumerator collectively. If you want to get all the values ​​of Map, use values ​​method.

Sample code



#### **`Sample code`**
```java

import java.util.HashMap;
import java.util.Map;

public class Sample {
           public static void main(String[] args) throws Exception {
                      Map<Integer, String> map = new HashMap<>();
 map.put (1, "Tanaka");
 map.put (3, "Suzuki");
 map.put (5, "Yamada");

                      for(String values: map.values()) {
                                 System.out.println(values);
                      }
           }
}

Execution result


 Tanaka
 Suzuki
 Yamada

#References [For beginners] Explain how to use Java map! Introducing sample code! [Introduction to Java] Summary of how to use Map(Initialize with HashMap, sort values)

Recommended Posts

[Java] How to use Map
[Java] How to use Map
How to use Java Map
How to use Map
How to use map
How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
How to use rbenv
[Java] How to use the hasNext function
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Rails] How to use the map method
How to use collection_select
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to use JUnit 5
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use Dozer.mapper
[Java] How to use the Calendar class
How to use Gradle