Skip to content
Sharp

Collections · interview question

Why might you choose a TreeMap over a HashMap for an in-memory store catalog?

Multiple choiceMediumchapter-4-curatedpremiumcollectionschoosing-the-right-collection
Choose one answer
Answer and explanation

Correct answer

C. Natural ordering of keys

Why

A TreeMap maintains a sorted order of keys, which can be beneficial if you need to frequently access elements in a sorted manner. A HashMap does not guarantee any order.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →