← Back to Java

Set vs List

JavaEntryjava

The Question

What is the difference between Set and List in Java?

What a Strong Answer Covers

  • List allows duplicates
  • Set no duplicates
  • List maintains insertion order
  • List has index access
  • HashSet unordered
  • LinkedHashSet insertion order
  • TreeSet sorted

Senior-Level Answer

List and Set are both sub-interfaces of Collection but with fundamentally different contracts around ordering and uniqueness.

A List is an ordered sequence. It maintains insertion order (or a deliberately sorted order), allows duplicate elements, and provides index-based access via get(int index). The primary implementations are ArrayList (random access, backed by an array, O(1) get) and LinkedList (sequential access, O(n) get, but O(1) insert/remove at a known position).

A Set models a mathematical set: no duplicate elements are permitted. The equals() and hashCode() contract determines what counts as a duplicate. HashSet offers O(1) average add/contains/remove but no ordering guarantee. LinkedHashSet preserves insertion order with O(1) operations. TreeSet maintains elements in natural or comparator-defined sorted order with O(log n) operations, implementing the NavigableSet interface.

The practical decision is: do you need uniqueness or do you need indexed positional access? If you need to store and retrieve by position, or duplicates are meaningful, use a List. If the semantics of your data require uniqueness (email addresses, active user IDs, feature flags), use a Set — it enforces the constraint automatically rather than relying on manual checks.

Performance difference for membership testing is significant. contains() on an ArrayList is O(n) because it scans every element. contains() on a HashSet is O(1) average. For large collections where membership checks are frequent, HashSet dramatically outperforms ArrayList.

Note that iterating a HashSet in a specific order is unreliable across JVM runs (hash seed randomization). If deterministic iteration order matters, prefer LinkedHashSet or TreeSet.

Key Differences

AspectListSet
DuplicatesAllowedNot allowed
OrderInsertion order preservedDepends on implementation (none/insertion/sorted)
Index accessYes — get(int index)No
contains() complexityO(n) for ArrayListO(1) avg for HashSet
Primary implementationsArrayList, LinkedListHashSet, LinkedHashSet, TreeSet
Use caseOrdered data, positional access, duplicates meaningfulUnique elements, fast membership testing

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly states List allows duplicates with ordered access and Set enforces uniqueness, and names at least one implementation of each.

3/3 — Strong Answer

Adds contains() performance comparison, distinguishes HashSet vs LinkedHashSet vs TreeSet ordering guarantees, and gives a concrete use-case rationale for choosing one over the other.

Common Mistakes

  • Saying Set has 'no order' without qualifying that LinkedHashSet preserves insertion order and TreeSet maintains sorted order.
  • Not knowing the O(n) vs O(1) contains() difference between ArrayList and HashSet, which is often the reason to choose Set.
  • Forgetting that Set relies on correct equals() and hashCode() implementations — objects with broken contracts will misbehave in Sets.

Follow-Up Questions

  • What happens when you add an object to a HashSet whose hashCode is always the same constant? — All objects hash to the same bucket, turning the HashSet into a linked list with O(n) performance for all operations.
  • When would you use TreeSet over HashSet? — When you need elements in sorted order or need range-view operations like headSet(), tailSet(), or subSet() from NavigableSet.
  • Can a List contain null elements? Can a Set? — ArrayList allows multiple nulls. HashSet and LinkedHashSet allow one null. TreeSet does not allow null because it calls compareTo, which throws NPE.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout