Introduction
The List
interface is a part of the Java Collections Framework and represents an ordered collection (also known as a sequence). Lists can contain duplicate elements, and the elements can be accessed by their integer index (position in the list). The List
interface extends the Collection
interface and is found in the java.util
package.
Table of Contents
- What is the List Interface?
- Key Points About List Interface
- Common Methods of List Interface
- Implementations of List Interface
- Usage Examples
- Conclusion
1. What is the List Interface?
The List
interface is an ordered collection that allows positional access and insertion of elements. Lists can have duplicate elements, and null elements are allowed (if the implementation supports them).
2. Key Points About List Interface
- Ordered Collection: Maintains the order of elements.
- Index-Based Access: Allows access to elements based on their position in the list.
- Duplicate Elements: Permits duplicate elements.
- Null Elements: Allows null elements (depending on the implementation).
3. Common() Methods of List Interface
Here are some of the common methods provided by the List
interface:
void add(int index, E element)
: Inserts the specified element at the specified position in the list.boolean add(E e)
: Appends the specified element to the end of the list.E get(int index)
: Returns the element at the specified position in the list.E set(int index, E element)
: Replaces the element at the specified position in the list with the specified element.E remove(int index)
: Removes the element at the specified position in the list.boolean remove(Object o)
: Removes the first occurrence of the specified element from the list.int indexOf(Object o)
: Returns the index of the first occurrence of the specified element in the list.int lastIndexOf(Object o)
: Returns the index of the last occurrence of the specified element in the list.ListIterator<E> listIterator()
: Returns a list iterator over the elements in the list.ListIterator<E> listIterator(int index)
: Returns a list iterator over the elements in the list, starting at the specified position in the list.List<E> subList(int fromIndex, int toIndex)
: Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
4. Implementations of List Interface
The List
interface is implemented by several classes in Java:
4.1 ArrayList
An ArrayList
is a resizable array implementation of the List
interface. It allows fast random access to elements and provides better performance for frequently read operations.
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("ArrayList: " + fruits);
}
}
Output:
ArrayList: [Apple, Banana, Cherry]
4.2 LinkedList
A LinkedList
is a doubly-linked list implementation of the List
and Deque
interfaces. It provides better performance for add and remove operations compared to ArrayList
.
import java.util.LinkedList;
import java.util.List;
public class LinkedListExample {
public static void main(String[] args) {
List<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("LinkedList: " + fruits);
}
}
Output:
LinkedList: [Apple, Banana, Cherry]
4.3 Vector
A Vector
is a synchronized, resizable array implementation of the List
interface. It is similar to ArrayList
, but it is synchronized, making it thread-safe.
import java.util.List;
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
List<String> fruits = new Vector<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("Vector: " + fruits);
}
}
Output:
Vector: [Apple, Banana, Cherry]
4.4 Stack
A Stack
is a subclass of Vector
that represents a last-in, first-out (LIFO) stack of objects. It provides methods for pushing and popping elements from the stack.
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");
System.out.println("Stack: " + stack);
System.out.println("Popped element: " + stack.pop());
System.out.println("Stack after pop: " + stack);
}
}
Output:
Stack: [Apple, Banana, Cherry]
Popped element: Cherry
Stack after pop: [Apple, Banana]
5. Usage Examples
Example 1: Using an ArrayList
import java.util.ArrayList;
import java.util.List;
public class ArrayListUsageExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Accessing elements
System.out.println("First fruit: " + fruits.get(0));
// Modifying elements
fruits.set(1, "Blueberry");
System.out.println("Modified List: " + fruits);
// Removing elements
fruits.remove(2);
System.out.println("After removal: " + fruits);
// Iterating over the list
System.out.println("Iterating over the list:");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Output:
First fruit: Apple
Modified List: [Apple, Blueberry, Cherry]
After removal: [Apple, Blueberry]
Iterating over the list:
Apple
Blueberry
Example 2: Using a LinkedList
import java.util.LinkedList;
import java.util.List;
public class LinkedListUsageExample {
public static void main(String[] args) {
List<String> fruits = new LinkedList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Accessing elements
System.out.println("First fruit: " + fruits.get(0));
// Modifying elements
fruits.set(1, "Blueberry");
System.out.println("Modified List: " + fruits);
// Removing elements
fruits.remove(2);
System.out.println("After removal: " + fruits);
// Iterating over the list
System.out.println("Iterating over the list:");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Output:
First fruit: Apple
Modified List: [Apple, Blueberry, Cherry]
After removal: [Apple, Blueberry]
Iterating over the list:
Apple
Blueberry
Example 3: Using a Vector
import java.util.List;
import java.util.Vector;
public class VectorUsageExample {
public static void main(String[] args) {
List<String> fruits = new Vector<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Accessing elements
System.out.println("First fruit: " + fruits.get(0));
// Modifying elements
fruits.set(1, "Blueberry");
System.out.println("Modified List: " + fruits);
// Removing elements
fruits.remove(2);
System.out.println("After removal: " + fruits);
// Iterating over the list
System.out.println("Iterating over the list:");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
Output:
First fruit: Apple
Modified List: [Apple, Blueberry, Cherry]
After removal: [Apple, Blueberry]
Iterating over the list:
Apple
Blueberry
Example 4: Using a Stack
import java.util.Stack;
public class StackUsageExample {
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");
// Accessing elements
System.out.println("Top element: " + stack.peek());
// Popping elements
System.out.println("Popped element: " + stack.pop());
System.out.println("Stack after pop: " + stack);
// Iterating over the stack
System.out.println("Iterating over the stack:");
for (String fruit : stack) {
System.out.println(fruit);
}
}
}
Output:
Top element: Cherry
Popped element: Cherry
Stack after pop: [Apple, Banana]
Iterating over the stack:
Apple
Banana
6. Conclusion
The List
interface in Java is a powerful and flexible way to handle ordered collections of elements. It provides a wide range of methods to manipulate and access the elements in the list.
By understanding the List
interface and its implementations, you can choose the appropriate list type for your specific use case, whether you need fast access to elements, efficient insertions and deletions, or thread safety.