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.
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".
Map<Key model name,Value type name>Object name= new HashMap<>();
//Example
Map<String, String> map = new HashMap<>();
map.put("Apple", "apple");
map.put("Orange", "orange");
map.put("banana", "banana");
System.out.println(map.get("Apple"));
//Output result apple
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);
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. ..
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
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.
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.
It was organized by writing in Qiita. Maybe this made me a little better with Map? !!
・ 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