List in Java
Introduction
A List in Java is an ordered collection that allows us to store multiple elements. A List can contain duplicate values and maintains the insertion order.
The List interface is part of the Java Collection Framework and is available in the java.util package.
import java.util.List;
Features of List
- Maintains insertion order.
- Allows duplicate elements.
- Allows
nullvalues. - Elements can be accessed using an index.
- Supports dynamic size.
- Provides methods to add, remove, and update elements.
List Syntax
List<Type> listName = new ArrayList<>();
Example:
List<String> names = new ArrayList<>();
Example of List
import java.util.*; public class ListExample { public static void main(String[] args) { List<String> names = new ArrayList<>(); names.add("Anu"); names.add("Ram"); names.add("Sita"); names.add("Anu"); System.out.println(names); } }
Output
[Anu, Ram, Sita, Anu]
Here, "Anu" appears twice because a List allows duplicate values.
Common List Methods
| Method | Description |
|---|---|
add() | Adds an element |
get() | Returns an element |
set() | Updates an element |
remove() | Removes an element |
size() | Returns number of elements |
contains() | Checks whether an element exists |
clear() | Removes all elements |
isEmpty() | Checks whether the List is empty |
Example
List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Mango"); fruits.add("Banana"); System.out.println(fruits.get(1)); fruits.set(1, "Orange"); fruits.remove("Banana"); System.out.println(fruits);
Output
Mango [Apple, Orange]
Accessing List Elements
List elements can be accessed using their index.
List<String> cities = new ArrayList<>(); cities.add("Janakpur"); cities.add("Kathmandu"); cities.add("Pokhara"); System.out.println(cities.get(0)); System.out.println(cities.get(1));
Output
Janakpur Kathmandu
Note: List indexing starts from 0.
Types of List
Common implementations of the List interface are:
1. ArrayList
ArrayList is commonly used when frequent access to elements is required.
List<String> names = new ArrayList<>(); names.add("Anu"); names.add("Ram"); System.out.println(names);
2. LinkedList
LinkedList is useful when elements are frequently added or removed.
List<Integer> numbers = new LinkedList<>(); numbers.add(10); numbers.add(20); numbers.add(30); System.out.println(numbers);
3. Vector
Vector is a synchronized List implementation.
List<Integer> numbers = new Vector<>(); numbers.add(10); numbers.add(20); System.out.println(numbers);
List vs Array
| List | Array |
|---|---|
| Dynamic size | Fixed size |
| Part of Collection Framework | Basic Java data structure |
| Provides many built-in methods | Fewer built-in methods |
| Allows easy insertion/removal | Insertion/removal is less flexible |
| Can store objects using generics | Can store primitive types directly |
Advantages of List
- Maintains insertion order.
- Allows duplicate elements.
- Provides dynamic size.
- Provides many useful methods.
- Elements can be accessed using indexes.
- Easy to add, remove, and update elements.
Real-Life Example
A List can be used to store:
Student Names -------------Anu Ram Sita Hari Anu
The duplicate name "Anu" is allowed because List supports duplicate elements.
Key Points
Listis an interface in Java.- It belongs to the
java.utilpackage. - It maintains insertion order.
- It allows duplicate values.
ArrayList,LinkedList, andVectorare common implementations.- List indexing starts from
0. - List provides methods such as
add(),get(),set(), andremove().
Conclusion
A List is one of the most commonly used collections in Java. It is useful when we need an ordered collection that can grow or shrink dynamically and can contain duplicate elements. ArrayList is generally a good starting choice for beginners.