bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java How To's
Java•Java How To's

Java How To Loop Through a HashMap

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java How To Loop Through a HashMap?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// ___ keys

Loop Through a HashMap

Loop through the items of a HashMap with a for-each loop.

Note

Use the keySet() method if you only want the keys, and use the values() method if you only want the values:

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));
}

Related Pages

Java HashMap Methods

Previous

Java How To Loop Through an ArrayList

Next

Java How To Loop Through an Enum