EasyExperience level: 0-2 years · 3-5 years1 min read

How do you choose between List, Set and Map for a new feature?

Reviewed by Gurusankar M.

Asked inInfosysTCSCognizantAccentureWipro
#list#set#map#collection choice#data structures
Report issue

Short Answer

List = ordered, allows duplicates, index access. Set = unique elements, membership tests. Map = key→value lookups. Pick by the question you ask most: 'what's at position i?' (List), 'have I seen this?' (Set), 'what's the value for this key?' (Map).

On this page

Concept Made Simple

Coffee Chat Question

“How do you choose between List, Set and Map for a new feature?”

Remember It Faster

Mind Map Answer

List (ArrayList)
ordered, duplicates, O(1) index
Set (HashSet)
unique, O(1) contains
Map (HashMap)
key→value, O(1) get
Need order?
LinkedHashSet/Map or TreeSet/Map

Think Beyond the Expected

What If?

You need uniqueness AND insertion order — which collection?

LinkedHashSet: it rejects duplicates like HashSet but preserves insertion order via a linked list, so iteration is predictable — ideal for de-duplicating a stream while keeping first-seen order.

Real World

Deduplicating user roles → Set; an ordered cart of line items → List; looking up a product by SKU → Map. Picking wrong (e.g. List.contains in a hot path) is a top cause of O(n²) slowness.

Interviewer's Expectation

Keywords they're listening for:

  • duplicates vs uniqueness
  • index vs key vs membership
  • ordering variants
  • Big-O of contains/get

Common Mistakes

  • Using List.contains() for membership in a loop (O(n²))
  • Using a Map when a Set (keys only) suffices
  • Assuming HashMap/HashSet preserve order

Best Practices

  • Match the structure to your dominant access pattern
  • Use a Set for membership, not List.contains
  • Reach for Linked*/Tree* variants only when you need ordering

Follow-up Questions

  1. When does List.contains() become a performance problem?
  2. How do you keep insertion order in a Set or Map?
  3. When would you use a sorted structure (TreeMap/TreeSet)?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: List vs Set vs Map (Java Collections)
Interview question: "How do you choose between List, Set and Map for a new feature?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.