What and Why List interface
The List interface in Java is used when you need to store a collection of elements in a specific order that allows duplicate elements. It is part of the Java Collections Framework and provides a flexible and powerful way to manage and manipulate ordered collections.
Here are some reasons why you would use the List interface in Java:
- Order Preservation: The
Listinterface maintains the order of elements as they are inserted. This allows you to access elements by their index and perform operations like appending, inserting, and removing elements at specific positions. - Indexed Access:
Listprovides direct access to elements based on their index using methods likeget(int index). This makes it convenient to retrieve and modify elements at specific positions within the list. - Duplicate Elements: Unlike other collection types like
Set,Listallows duplicate elements. You can add multiple occurrences of the same element to the list, and they will be stored as separate entries. - Dynamic Size:
Listimplementations, such asArrayListorLinkedList, automatically handle resizing as elements are added or removed. This allows you to dynamically change the size of the list without needing to manage the underlying data structure manually. - Iteration and Manipulation: The
Listinterface provides several methods to iterate over elements, such as enhanced for loop,Iterator, orListIterator. It also offers numerous methods to add, remove, or modify elements, allowing you to perform various operations on the collection. - Compatibility with Algorithms: The
Listinterface is widely used in Java libraries and APIs. It is compatible with numerous algorithms, sorting methods, and utility classes provided by the Java Collections Framework. It allows you to leverage the rich functionality available for working with ordered collections. - Flexibility: The
Listinterface has various implementations to choose from, depending on your specific requirements. For example,ArrayListprovides fast element access and manipulation, whileLinkedListoffers efficient insertion and removal at both ends of the list.
Overall, the List interface is suitable when you need to manage ordered collections that allow duplicates and require frequent access to elements based on their position. It provides a rich set of methods and compatibility with existing Java libraries, making it a versatile choice for many use cases.
List interface syntax as in Java API
The syntax of the List interface in the Java API is as follows :
public interface List<E> extends Collection<E> {
// Positional Access
E get(int index);
E set(int index, E element); // inserts and returns previous element at index.
void add(int index, E element);
void add(E element); // adds to the end of list
E remove(int index);
boolean addAll(int index, Collection<? extends E> c); // add all from Collection
// Search Operations
int indexOf(Object o); // returns -1 if object not found
int lastIndexOf(Object o);
// List Iterators
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
// View Operations
List<E> subList(int fromIndex, int toIndex);
}
In the above syntax, the List interface is defined as a generic interface (List<E>) that can work with elements of any specific type represented by the type parameter E.
The interface extends the Collection interface, inheriting its methods and adding additional methods specific to lists.
The List interface includes methods categorized into positional access, search operations, list iterators, and view operations.
The positional access methods include get(int index), set(int index, E element), add(int index, E element), and remove(int index). These methods allow you to retrieve an element at a specific index, modify the element at a given index, insert an element at a particular index, and remove an element at a specific index, respectively.
The search operations methods are indexOf(Object o) and lastIndexOf(Object o). These methods return the index of the first occurrence and the last occurrence of the specified object in the list, respectively. If the object is not found, these methods return -1.
The list iterator methods are listIterator() and listIterator(int index). These methods return a ListIterator that allows bidirectional iteration over the elements of the list. The second variant allows you to specify the starting position for the iterator.
- ListIterator is a sub interface of Iterator interface.
The range view operation method is subList(int fromIndex, int toIndex). This method returns a view of the list between the specified fromIndex (inclusive) and toIndex (exclusive).
- Returned list is backed by the original list, meaning any change made in the returned list will be reflected in original list and vice versa. Changes to the sublist will be reflected in the original list and vice versa.
- ** To the returned list, we can do both data related changes(meaning updating data) or structural changes like adding / removing elements etc. The changes will get reflected in original list. However, to the original list, if we do any structural changes like adding / removing elements, then the returned list which is the view, will be invalidated and trying to access elements using returned list will throw ConcurrentModificationException.
Note that the List interface inherits other methods from the Collection interface, such as add(E e), remove(Object o), contains(Object o), size(), isEmpty(), addAll(Collection<? extends E> c), removeAll(Collection<?> c), retainAll(Collection<?> c), containsAll(Collection<?> c), toArray(), and toArray(T[] a).
The List interface serves as a contract for list implementations and provides a wide range of methods to manage ordered collections.