020-003-012
Iterating with Map.entrySet()
Medium
Problem Description
Iterating with Map.entrySet()
In this problem: You will use Map.entrySet() to iterate over all key-value pairs in a HashMap and display the results to standard output.
Learning Objective: Understand efficient map iteration using Map.entrySet()
Overview
entrySet() returns all entries (key-value pairs) as a Set. Use the enhanced for loop to iterate and access each entry's key and value.
Specifications
- Create a
LinkedHashMap<String, Integer>(to maintain insertion order) - Add student names and scores:
"Alice"=85,"Bob"=92,"Charlie"=78 - Use
entrySet()with enhanced for loop to output all entries as"{name}: {score}" - Calculate and output the total and average of all scores
Output Format
Alice: 85
Bob: 92
Charlie: 78
Total: 255
Average: 85.0
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up