InterviewVault
Welcome back, Sujit Kumar Mishra
Admin
SK Mishra
Revision Mode
Document technical questions and best-practice answers.
What are the difference between ArrayList and HashSet?
Difference between ArrayList and HashSet:
1: Duplicates
ArrayList allows duplicate elements.
HashSet does NOT allow duplicate elements.
2: Order
ArrayList maintains the order in which you add elements (insertion order).
HashSet does NOT guarantee any order.
3: Performance
ArrayList is better if you need to access elements by their index (like getting the 3rd item).
HashSet is faster for checking if an element exists.
In short:
- Use ArrayList when you want an ordered list that can have duplicates.
- Use HashSet when you want a collection with unique items and don’t care about the order.