[Java] How to get HashMap elements by loop control using extended for statement

Development environment

-Host OS: Windows10 Home ・ Guest OS: WSL2 Ubuntu20.04 LTS ・ VScode ver 1.44.2 ・ Openjdk 11.0.7

Overview

-Get each element of HashMap using the extended for statement. The statement is a bit more complicated than an array or list.

Sample code


HashMap<String, Integer> map = new HashMap<String, Integer>();

map.put("dog", 3);
map.put("Cat", 10);
map.put("rabbit", 5);

① Get the key

Use the ** keySet () ** method


for(String animal : map.keySet()) {
  System.outprintln(animal);
}

Output result

Cat
rabbit
dog

(* HashMap is not guaranteed in order unlike arrays and lists)

② Get value

Use the ** values () ** method. Note that it's not the valuesSet () method


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

Output result

5
3
10

Get both key and value

Use the ** entrySet () ** method.


for(Map.Entry<String, Integer> entry : map.entrySet()) {
  System.out.println(entry.getKey() + ":" + entry.getValue());
}

Output result

Cat:10
dog:3
rabbit:5

Recommended Posts

[Java] How to get HashMap elements by loop control using extended for statement
How to loop Java Map (for Each / extended for statement)
[Java] Get Map key / value pairs using extended for statement
How to get the contents of Map using for statement Memorandum
How to make a groundbreaking diamond using Java for statement wwww
[Java] How to turn a two-dimensional array with an extended for statement
[Java] for statement / extended for statement
[Java] for statement / extended for statement
[Java] How to turn a two-dimensional array with an extended for statement
[Java] How to get HashMap elements by loop control using extended for statement
Compare the speed of the for statement and the extended for statement.
How to use an array for HashMap keys
Java for statement
I tried using an extended for statement in Java
[Beginner] Java variable / logical operator / extended for statement [Note 22]
How to loop Java Map (for Each / extended for statement)
[Java] How to get the maximum value of HashMap
[Java] How to get a request by HTTP communication
I tried using an extended for statement in Java
How to output array values without using a for statement
How to use Java HttpClient (Get)
[Java] How to use the HashMap class
[Java] How to calculate age using LocalDate
[Processing × Java] How to use the loop
[Java] How to get the current directory
[Java] How to get to the front of a specific string using the String class
How to get the date in java
[Java] [For beginners] How to insert elements directly in a 2D array
How to concatenate strings separated by commas using StringJoiner even in java7 environment
[Java] How to easily get the longest character string of ArrayList using stream
[Java] How to test for null with JUnit
[Java] How to get and output standard input
How to use an array for HashMap keys
How to get Class from Element in Java
[Java] (for MacOS) How to set the classpath
[Java] How to get random numbers excluding specific numbers
How to get and study java SE8 Gold
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
How to specify index of JavaScript for statement
[Java] How to operate List using Stream API
[Java] How to make multiple for loops single
Java for statement
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Java] How to get the URL of the transition source
[Java] [SQL Server] Connect to local SQL Server 2017 using JDBC for SQL Server
Rails / Ruby: How to get HTML text for Mail
[For beginners] How to operate Stream API after Java 8
How to delete custom Adapter elements using a custom model
How to use Truth (assertion library for Java / Android)
As of April 2018 How to get Java 8 on Mac
[Java] How to cut out a character string character by character
How to convert A to a and a to A using AND and OR in Java
[Beginner] Java variable / logical operator / extended for statement [Note 22]
How to execute WebCamCapture sample of NyARToolkit for Java
[Java] for statement, while statement
(Memo) Java for statement
How to get the class name / method name running in Java
[Java] Make variables in extended for statement and for Each statement immutable
An unsupported Java version How to get rid of errors
How to use SAS tokens for Azure Event hubs (Java)
How to create a lightweight container image for Java apps
[Processing × Java] How to use loop 2 --- Nested structure, coordinate conversion
[Rails] How to delete images uploaded by carrierwave (using devise)
Java program to get ESS Job log by Web API
Try Spark Submit to EMR using AWS SDK for Java
For Java beginners: List, Map, Iterator / Array ... How to convert?
How to get started with JDBC using PostgresSQL on MacOS
Investigated how to call services with Watson SDK for Java