Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Loop Through a HashMap

Loop Through a HashMap

Traverse through the entries of a HashMap utilizing a for-each loop.

Note: Utilize the keySet() method for keys only, and the values() method for values only.

Example

// Print keys
for (String i : capitalCities.keySet()) {
  System.out.println(i);
}

Example

// Print values
for (String i : capitalCities.values()) {
  System.out.println(i);
}

Example

// Print keys and values
for (String i : capitalCities.keySet()) {
  System.out.println("key: " + i + " value: " + capitalCities.get(i));
}