List in Java
Source: Dev.to
What is a List?
List is an interface in the Java Collection Framework that stores an ordered collection of elements and allows duplicates. Elements can be accessed by their index.
- Maintains insertion order
- Allows duplicate elements
- Supports index‑based access
Why List?
- Order is important – data maintains insertion order.
- Duplicates are allowed – e.g., the same product added twice in a cart.
- Index‑based access needed – you can retrieve elements using an index.
When to Use a List?
- When the number of elements is unknown.
- When duplicates are allowed.
- When ordered data is required.
- When index‑based operations are needed.
Real‑Time Examples
- E‑commerce Cart – Use a List to store products in the cart (order matters).
- Student Marks – Store multiple marks, allowing duplicates.
- Log History – Maintain insertion order of log entries.