Java List
Java List Interface
The List interface is part of the Java Collections Framework and represents an ordered collection of elements.
You can access elements by their index, add duplicates, and maintain the insertion order.
Since List is an interface, you cannot create a List object directly.
Instead, you use a class that implements the List interface, such as:
ArrayList- like a resizable array with fast random accessLinkedList- like a train of cars you can easily attach or remove
Tip: Use List when you care about order, you may have duplicates, and want to access elements by index.
Common List Methods
| Method | Description |
|---|---|
add() |
Adds an element to the end of the list |
get() |
Returns the element at the specified position |
set() |
Replaces the element at the specified position |
remove() |
Removes the element at the specified position |
size() |
Returns the number of elements in the list |
List vs. Array
| Array | List |
|---|---|
| Fixed size | Dynamic size |
| Faster performance for raw data | More flexible and feature-rich |
| Not part of Collections Framework | Part of the Collections Framework |
In the next chapters, you will learn how to use ArrayList and LinkedList.