Difference between List and Set in Java
๐ก Concept: List vs Set
List
and Set
are two key interfaces in Java's Collection Framework with different characteristics: Lists allow duplicates and preserve insertion order, while Sets do not allow duplicates and may not preserve order.
๐ Quick Intro
Use List
when order and duplicates matter. Use Set
when uniqueness is required. Examples: ArrayList vs HashSet.
๐ง Analogy
A List
is like a queue of people โ order matters and people can appear more than once. A Set
is like a guest list โ each name is unique, and order is not always guaranteed.
๐ง Technical Explanation
List
allows duplicates;Set
does not.List
maintains insertion order;Set
does not (except LinkedHashSet).List
supports positional access;Set
does not.Set
is generally faster for search due to hashed structure.- Common implementations:
ArrayList
,LinkedList
,HashSet
,TreeSet
.
๐ฏ Use Cases
- โ
Use
List
when duplicates and order are important (e.g., tasks, playlists). - โ
Use
Set
to maintain a unique collection (e.g., unique usernames). - โ
Use
TreeSet
for sorted data without duplicates.
๐ป Example: List vs Set
import java.util.*;
public class ListSetExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("A"); list.add("B"); list.add("A");
System.out.println("List: " + list); // Allows duplicates
Set<String> set = new HashSet<>();
set.add("A"); set.add("B"); set.add("A");
System.out.println("Set: " + set); // Removes duplicates
}
}

โ Interview Q&A
Q1: Can List have duplicates?
A: Yes.
Q2: Does Set allow duplicates?
A: No.
Q3: Which maintains insertion order?
A: List and LinkedHashSet.
Q4: Is positional access supported in Set?
A: No.
Q5: Which is faster for contains()?
A: Set (especially HashSet).
Q6: Can we sort a Set?
A: Yes, with TreeSet.
Q7: Which is better for storing tasks in order?
A: List.
Q8: Which should be used for unique IDs?
A: Set.
Q9: Does List guarantee uniqueness?
A: No.
Q10: Can we iterate both using for-each loop?
A: Yes.
๐ MCQs
Q1. Which allows duplicates?
- Set
- List
- TreeSet
- LinkedHashSet
Q2. Which does not maintain order?
- ArrayList
- LinkedList
- HashSet
- LinkedHashSet
Q3. Which supports positional access?
- Set
- List
- HashSet
- TreeSet
Q4. Which ensures unique values?
- List
- Set
- ArrayList
- Vector
Q5. Which is typically faster for lookup?
- ArrayList
- HashSet
- TreeMap
- Stack
Q6. Which class maintains sorted order?
- List
- HashSet
- TreeSet
- ArrayList
Q7. Which is ideal for ordered duplicates?
- Set
- TreeSet
- List
- Map
Q8. Does HashSet allow null?
- Yes
- No
- Only once
- Never
Q9. Which maintains insertion order?
- HashSet
- LinkedHashSet
- TreeSet
- HashMap
Q10. Which does not allow duplicates?
- List
- ArrayList
- Set
- Vector
๐ก Bonus Insight
While both List and Set are collections, choosing between them impacts performance, memory usage, and behavior significantly depending on your application needs.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!