Skip to content
Sharp

Collections · interview question

In what scenario would you use containsKey() over get() when working with a HashMap?

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

Correct answer

A. When you want to check if a key exists without retrieving its value.

Why

containsKey() is used to check if a specific key exists in the map, whereas get() retrieves the value associated with a key. Use containsKey() when the existence of a key is more important than its value.

Test yourself on Collections

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

Start free test
All Collections questions →