I want to get along with Map [Java beginner]

Introduction

It's been 5 months since I've been in Java (Is it so long?). I would like to get along with Map, which I have avoided until now.

During the three months of training, I came with the Only ArrayList. Still, it worked.

However, when I first entered the field, I didn't think that Map was used so much. I didn't use it too much, so put? It became. It's too dangerous.

There are many articles that summarize Map, I would like to summarize the parts that I thought were easy to understand even though I was not good at Map.

What is Map?

Multiple data can be stored by pairing a key and a value. It's like a dictionary where you can find the "value" that corresponds to the "key".

Well, how do you say apples in English? .. You can use Map to find the value "apple" with the key "apple".

Let's use Map! Initialize

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

//Example
Map<String, String> map = new HashMap<>();

Add key / value pairs: put

map.put("Apple", "apple");
map.put("Orange", "orange");
map.put("banana", "banana");

Get the value corresponding to the key: get

System.out.println(map.get("Apple"));
//Output result apple

Get value with extended for statement

I put some data in the map, so I want to display all using the for statement. This part was my weak point. From here, we will use the key as the product name and the value as the price so that the key and value are easy to understand.

Map<String, Integer> fruits = new HashMap<>();
fruits.put("Apple", 128);
fruits.put("Orange", 98);
fruits.put("banana", 198);

I want to display both keys and values: entrySet, keySet

for (Map.Entry<String, Integer> entry : fruits.entrySet()) {
  System.out.println(entry.getKey() + "Is" + entry.getValue() + "It is a circle.");
}

//Output result
//Apples are 128 yen.
//Orange is 98 yen.
//Bananas are 198 yen.

What is Entry! I hate it because it's too long! !!

An entry is a key / value pair. entrySet is a method to get all entries.

You can write the same thing more simply with keySet. keySet is a method to get all the keys.

for (String fruit : fruits.keySet()) {
  System.out.println(fruit + "Is" + fruits.get(fruit) + "It is a circle.");
}

//Output result
//Apples are 128 yen.
//Orange is 98 yen.
//Bananas are 198 yen.

Then I should remember this much. Textbooks, why write confusing things first. ..

When only values ​​are needed: values

Well, how much is an apple? I just want to know the price.

for (int value : fruits.values()) {
  System.out.println(value + "Circle");
}

//Output result
//128 yen
//98 yen
//198 yen

Does it contain that element: containKey, containValue

if (fruits.containsKey("banana")) {
  System.out.println("Bananas are recommended.");
}

//Output result Banana is recommended.

if (fruits.containsValue(98)) {
  System.out.println("It is a bargain.");
} 

//Output result is a bargain.

Other methods: size, remove, clear, isEmpty

Get the number of elements (size), remove elements (remove), remove all elements (clear), This place is the same as other collections such as List. Use the product name and price map from earlier.

System.out.println(fruits.size());

//Output result 3
//Deleted "Key: Apple, Value: 128". Arguments are OK with just the key
fruits.remove("Apple");
//Removed all elements in the map. No arguments required.
fruits.clear();
if (fruits.isEmpty()) {
  System.out.println("I'm sorry. It's sold out today.");
}

//Output result I'm sorry. It's sold out today.

at the end

It was organized by writing in Qiita. Maybe this made me a little better with Map? !!

reference

Java Language Programming Lesson 3rd Edition (Bottom) Let's Start Object Oriented Hiroshi Yuki (Author) ・ Https://www.tech-teacher.jp/blog/java-map/

Recommended Posts

I want to get along with Map [Java beginner]
I want to use java8 forEach with index
I want to transition screens with kotlin and java!
I tried to interact with Java
How to get along with Rails
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to implement various functions with kotlin and java!
[Java] I want to test standard input & standard output with JUnit
[Java] Get List / Map elements with Iterator
I want to return to the previous screen with kotlin and java!
I want to use DBViewer with Eclipse 2018-12! !!
I want to stop Java updates altogether
[Java] I want to perform distinct with the key in the object
I want to display images with REST Controller of Java and Spring!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I want to create a dark web SNS with Jakarta EE 8 with Java 11
[Java] [ibatis] How to get records of 1-to-N relationship with List <Map <>>
I want to get the IP address when connecting to Wi-Fi in Java
I want to ForEach an array with a Lambda expression in Java
I want to get the field name of the [Java] field. (Old tale tone)
I want to test Action Cable with RSpec test
Run R from Java I want to run rJava
I tried to make Basic authentication with Java
I want to send an email in Java.
I want to play with Firestore from Rails
I want to write quickly from java to sqlite
I tried to break a block with java (1)
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
Get along with Java containers in Cloud Run
I want to get the value in Ruby
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to do something like "cls" in Java
[Java] I want to calculate the difference from the date
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I tried to get started with Spring Data JPA
I want to dark mode with the SWT app
A story about trying to get along with Mockito
I want to authenticate users to Rails with Devise + OmniAuth
I tried to implement Stalin sort with Java Collector
I used to make nc (netcat) with JAVA normally
I want to redirect sound from Ubuntu with xrdp
I want to return an object in CSV format with multi-line header & filter in Java
# 1_JAVA I want to get the index number by specifying one character in the character string.
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
[Java] How to use Map
[Java] How to use Map
Java to play with Function
I want to convert characters ...
How to use Java Map
Connect to DB with Java
Connect to MySQL 8 with Java
[Beginner] I want to modify the migration file-How to use rollback-
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.