LearnFrenzy
Sign in Take a free test

HashMap and TreeMap · HashMap and TreeMap MCQ · Question 15

Iterating keys and values together Answer, explanation & discussion

  • MCQ
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 15 Back to Exercise

Which is the most efficient way to iterate over both keys and values?

  • A for (K k : map.keySet()) map.get(k)
  • B for (Map.Entry<K, V> e : map.entrySet()) Correct
  • C for (V v : map.values())
  • D for (int i = 0; i < map.size(); i++)
Correct Answer: Option B

keySet + get does an extra lookup per key.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links