Skip to content
Sharp

Collections · interview question

Which of the following is a valid reason to use a TreeMap over a HashMap?

Multiple choiceMediumchapter-4-curatedpremiumcollectionsmap-interface-and-hashmap
Choose one answer
Answer and explanation

Correct answer

A. You need keys to be sorted.

Why

A TreeMap provides a sorted order of keys, unlike a HashMap which does not guarantee any order. This makes TreeMap suitable when sorted keys are required.

Test yourself on Collections

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

Start free test
All Collections questions →