[Java] Get Map key / value pairs using extended for statement

Introduction

How to get a Map key / value pair using Map.entrySet. There are other ways to get Map keys and values: keySet and values. These only get either the key or the value, but you can use entrySet to get the key / value pair.

How to use entry.Set

Set the value in Map

		Map<String,String> animal = new HashMap<>();
		animal.put("monkey", "monkey");
		animal.put("dog", "dog");
		animal.put("cat", "Cat");

Get Map key / value pairs

In the following process, the entrySet method is used to get the key / value pair, the getKey method is used to get the key, and the getValue method is used to get the value.

	      for (Map.Entry<String, String> animalNameInJapanese : animal.entrySet()) {

            System.out.println(animalName.getKey() + "Is in Japanese" + animalName.getValue() + "is.");
        }

output

monkey is a monkey in Japanese.
dog is a dog in Japanese.
cat is a cat in Japanese.

reference

Map Java Platform SE8 #entrySet

Recommended Posts

[Java] Get Map key / value pairs using extended for statement
[Java] for statement / extended for statement
I tried using an extended for statement in Java
How to loop Java Map (for Each / extended for statement)
[Java] How to get HashMap elements by loop control using extended for statement
How to get the contents of Map using for statement Memorandum
Reverse Key from Value in Java Map
Java for statement
[Java] for statement, while statement
(Memo) Java for statement
[Beginner] Java variable / logical operator / extended for statement [Note 22]
[Java] How to get the key and value stored in Map by iterative processing
Store in Java 2D map and turn with for statement
[Java] Make variables in extended for statement and for Each statement immutable
[Java] Basic statement for beginners
Utilization of Java array elements, for, length, value, and extended for statements
How to make a groundbreaking diamond using Java for statement wwww
Get Null-safe Map values in Java
[Java] How to turn a two-dimensional array with an extended for statement
[Java] Nowadays, the extended for statement is not exclusively for List, isn't it?
Use MyBatis to get Map with key as identifier and value as Entity
[Java] Get List / Map elements with Iterator
Duplicate Map sorted by key in Java
Memory measurement for Java apps using jstat
Key points for introducing gRPC in Java
Map without using an array in java
Study Java Try using Scanner or Map
Java, for statement / while statement starting from beginner
Modern Java environment for Windows using Chocolatey