Collections · interview question
If you need to frequently check the existence of items in a catalog by their names, which collection offers the best performance?
Multiple choiceHardchapter-4-curatedpremiumcollectionschoosing-the-right-collection
Answer and explanation
Correct answer
D. HashSet
Why
HashSet provides constant time complexity O(1) for lookup operations, which is optimal for frequently checking the existence of elements. TreeSet, while sorted, has higher lookup time complexity of O(log n).
Test yourself on Collections
A free test with instant, explained results. No account needed.
More Collections questions
- Why do teams use a HashSet over a List when storing items in production code?
- Which Java collection should you use if you need to store elements with fast random access and allow duplicates?
- Which collection does not allow duplicate elements?
- Which Java collection would you choose if you need to maintain insertion order and allow duplicates?
- How would you explain a key advantage of using a HashMap over a TreeMap in an interview?
- You are tasked with designing a cache system in Java where both insertion order and fast access are crucial. Which…