Last Updated: 21 December, 2023
In this tutorial, we will discuss the List interface in Java and see the methods provided by the List interface, its implementations, and usage scenarios.
List is a part of the Java Collections Framework. It was introduced in the JDK 1.2 version and is available in the java.util package. The List interface extends the Collection interface, inheriting its methods and adding specific ones for list operations.
A List is an ordered collection of elements that allows us to store and access elements sequentially by their index. A List also allows us to insert null and duplicate elements.
The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement and bidirectional access in addition to the normal operations that the Iterator interface provides.
The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector. The ArrayList and LinkedList are widely used in Java programming. The Vector class has been deprecated since Java 5.
The complete List interface hierarchy can be seen in the previous section; the List interface extends the Collection interface, which in turn extends the Iterable interface.
Here, E defines the type of elements that the List will contain.
The List interface is implemented by ArrayList, LinkedList, Vectors and Stack classes in Java.
All the concrete classes of List Interface can access all the methods of List Interface.
Sun Microsystems kept all the common functionality in the List Interface to prevent duplicate methods and better control functionality.
As we already mentioned we cannot create an object of List Interface so here we are creating the reference object of List.
Output
List Elements: [India, Canada, US, England]
Method | Description |
---|---|
boolean add ( E e) | Adds the specified element to the end of the list. |
void add(int index, E element) | Inserts the specified element at the specified index in the list. |
boolean addAll(Collection<? extends E> c) | Adds all the elements in the specified collection to the end of the list, in the order returned by the iterators of the specified collection |
boolean addAll(int index, Collection<? extends E> c) | Inserts all elements in the specified collection at the specified position in the list. |
void clear() | Remove all elements from the list. |
boolean contains(Object o) | Returns true if the specified element is included in the list. |
boolean containsAll(Collection<?> c) | Returns true if all elements of the specified collection are included in the list. |
boolean equals(Object o) | Compares whether the specified object is equal to the list. |
E get(int index) | Returns the element at the specified position in the list. |
int hashCode() | Returns the hash code value for this list. |
int indexOf(Object o) | Returns the index of the position where the specified element was first found in this list. Returns -1 if the specified element is not in the list. |
boolean isEmpty() | Returns true if there are no elements in the list. |
Iterator<E> iterator() | Returns an iterator that iterates the elements in the list in the proper order. |
int lastIndexOf(Object o) | Returns the index of the position where the specified element was last found in this list. Returns -1 if the specified element is not in the list. |
ListIterator<E> listIterator() | Returns a list iterator that iterates through the elements in the list (in the proper order). |
ListIterator<E> listIterator(int index) | Returns a list iterator that iterates (in the proper order) the elements in the list, starting at the specified position in the list. |
E remove(int index) | Deletes the element at the specified position in the list. |
boolean remove(Object o) | If the specified element is in this list, remove the first one from the list. |
boolean removeAll(Collection<?> c) | Removes all elements from the specified collection from the list. |
default void replaceAll(UnaryOperator<E> operator) | Replace each element in the list with the result of applying the operator to that element. |
boolean retainAll(Collection<?> c) | Keeps only the elements contained in the specified collection in the list. |
E set(int index, E element) | Replaces the element at the specified position in the list with the specified element. |
int size() | Returns the number of elements in the list. |
default void sort(Comparator<? super E> c) | It has been designated Comparatorin accordance with, but the order shown, and then sort the list. |
default Spliterator <E> spliterator () | SpliteratorCreate for the elements in this list . |
List<E> subList(int fromIndex, int toIndex) | Returns a view of the part of the list from the specified fromIndex (included) to toIndex (not included). |
Object[] toArray() | Returns an array containing all the elements in the list in the proper order (first element to last element). |
<T> T[] toArray(T[] a) | Returns an array containing all the elements in the list in the proper order (first element to last element). The run-time type of the returned array will be the type of the specified array. |
The Java List interface provided methods are implemented in the below given examples. Let's go through the examples one by one and understand the uses for each method.
Example 1: Methods for Adding elements in the list
Output
List elements: [100, 200, 400, 500]
List elements after adding elements at index 2 : [100, 200, 300, 400, 500]
List elements after adding another list : [100, 200, 300, 600, 700, 400, 500, 600, 700]
List elements : [A, B, C, D]
Example 2: Method for Accessing elements from the list
Output
List element at index 3 : South Africa
Example 3: Methods for Updating elements in a list
Output
List elements: [100, 200, 400, 400]
List after update the element: [100, 200, 300, 400]
Example 4: Methods for Removing elements from a list
Output
List elements: [Java, Hibernate, Kafka, MongoDB, Jenkins, MySQL, JSF, Spring, Microservices]
List after calling remove(int index) method: [Java, Hibernate, Kafka, Jenkins, MySQL, JSF, Spring, Microservices]
List after calling remove(Object o) method: [Java, Hibernate, Kafka, Jenkins, MySQL, Spring, Microservices]
List after calling removeAll(Collection c) method: [Java, Hibernate, Jenkins, Spring]
After all remove() methods, final list elements:
Hibernate
Spring
Example 5: Utility methods of List Interface
Output
true
false
false
List Size Before : 5
List Size After : 0
Reference: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
What is the difference between the List and Set interfaces in Java?
Ans. List and Set both interfaces are available in the
List | Set |
---|---|
A List is an ordered collection of elements. | A Set is an unordered collection of elements. |
List contains duplicate elements. | Set does not contain duplicate elements. |
List accepts any number of nulls as an element. | Set accepts only one null as an element. |
List allows positional access to elements. | Set does not allow positional access to elements. |
List has one legacy class called Vector. | Set does not have any legacy classes. |
List uses ListIterator for traversing the elements in any direction. | ListIterator does not work with Set. |
List implementation classes are ArrayList, LinkedList, Vector and Stack. | Set implementation classes are TreeSet, HashSet and LinkedHashSet. |
What are the different implementations of the List interface in Java?
Ans. In Java, List interface provides an ordered collection of elements. The List interface is implemented by several classes, each providing a different implementation for storing and manipulating a sequence of elements.
These are the most commonly used implementations of the List interface in Java. Each implementation has its own characteristics, so the choice depends on your specific requirements in terms of performance, thread safety, and usage patterns.
When should we use the List interface?
Ans. In Java programming, there are the following situations where we should List interfaces: