The Java List interface is a fundamental component of the Java Collections Framework. It is designed to represent an ordered collection of elements. Lists allow duplicate elements and maintain the insertion order. Elements in a list can be accessed and manipulated using their indexes, starting from 0 for the first element.
The List interface inherits several methods from the Collection interface, such as add(), remove(), contains(), isEmpty(), size(), and more.
The List interface provides additional methods to perform operations specific to lists, including:
The AbstractList class is an abstract class that provides a skeletal implementation of the List interface. The ArrayList, LinkedList, Vector, and CopyOnWriteArrayList classes are concrete classes that implement the List interface.
1. AbstractList Class
AbstractList
is an abstract class that implements some of the methods of the List
interface. It provides a skeletal implementation that can be extended to create custom list implementations.size()
, isEmpty()
, contains()
, and more, which can be overridden by subclasses.get(int index)
and iterator()
to provide specific functionalities.ArrayList
is a resizable array implementation of the List
interface.get(index)
).LinkedList
is a doubly linked list implementation of the List
interface.ArrayList
, it can grow and shrink dynamically.Vector
is a synchronized implementation of the List
interface.Vector
are synchronized, making it thread-safe and suitable for concurrent access.ArrayList
, but it doubles its size when it runs out of space, which can lead to higher memory consumption.ArrayList
in single-threaded scenarios.Vector
when you need a thread-safe list but are not concerned about performance overhead, or when using legacy code.CopyOnWriteArrayList
is a thread-safe variant of ArrayList
.Here’s a basic example using a LinkedList to illustrate the implementation and usage of its methods:
import java.util.LinkedList;
import java.util.List;
public class Example {
public static void main(String[] args) {
// Create a list of Integers
List numbers = new LinkedList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
// Add an element to the list at index 1
numbers.add(1, 4);
numbers.forEach(System.out::println); // prints 1 4 2 3
// Get the element at a specific index
int number = numbers.get(0);
System.out.println(number); // prints 1
// Check if the list contains a specific element
boolean contains4 = numbers.contains(4);
System.out.println(contains4); // prints true
// Remove an element from the list
int index = numbers.indexOf(3);
numbers.remove(index);
// Iterate over the list
numbers.forEach(System.out::println); // prints 1 4 2
}
}
The Example
class demonstrates the use of a LinkedList
in Java by performing various operations on a list of integers. It begins by creating a LinkedList
and adding the integers. 1
, 2
, and 3
. Then, it inserts 4
at index 1
, resulting in the list [1, 4, 2, 3]
. The class retrieves and prints the first element, checks for the presence of 4
, and removes the element 3
from the list. Finally, it prints the remaining elements, showcasing the basic functionalities of adding, inserting, accessing, checking for existence, and removing elements in a LinkedList
.
Overall, the List interface is a powerful and flexible component for representing ordered collections of elements in Java. It is a good choice for a wide variety of applications.