In Java, the List interface is a part of the Java Collections Framework that represents an ordered collection of elements. It allows duplicate values and null elements, and provides index-based operations to store, access, update, and manipulate data efficiently.
In this chapter, we will learn about the Java List interface, its features, methods, implementations such as ArrayList and LinkedList, and how it is used to manage collections of elements effectively.
The List interface is available in the java.util package and extends the Collection interface. It is designed to store elements in a sequential (ordered) manner and provides methods to perform operations like insertion, deletion, updating, and searching using indexes.
A List allows duplicate elements and also supports storing null values. It maintains the insertion order, meaning elements are retrieved in the same order as they were added.
Additionally, the List interface provides a ListIterator that allows traversal of elements in both forward and backward directions. Some commonly used implementation classes of the List interface include ArrayList, LinkedList, Stack, and Vector (although Vector is considered legacy and less commonly used today).
The List interface is declared in the java.util package and extends the Collection interface.
Here is the syntax:
The List interface does not have constructors because it is an interface. However, objects of List can be created using its implementation classes like ArrayList and LinkedList.
Here are the syntaxes:
The List interface provides various methods to perform operations such as adding, removing, updating, and accessing elements in an ordered collection.
| Method | Description |
|---|---|
| void add(int index, E element) | It is used to insert the specified element at the specified position in a list. |
| boolean add(E e) | It is used to append the specified element at the end of a list. |
| boolean addAll(Collection<? extends E> c) | It is used to append all of the elements in the specified collection to the end of a list. |
| boolean addAll(int index, Collection<? extends E> c) | It is used to append all the elements in the specified collection, starting at the specified position of the list. |
| void clear() | It is used to remove all of the elements from this list. |
| boolean equals(Object o) | It is used to compare the specified object with the elements of a list. |
| int hashcode() | It is used to return the hash code value for a list. |
| E get(int index) | It is used to fetch the element from the particular position of the list. |
| boolean isEmpty() | It returns true if the list is empty, otherwise false. |
| int lastIndexOf(Object o) | It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element. |
| Object[] toArray() | It is used to return an array containing all of the elements in this list in the correct order. |
| <T> T[] toArray(T[] a) | It is used to return an array containing all of the elements in this list in the correct order. |
| boolean contains(Object o) | It returns true if the list contains the specified element |
| boolean containsAll(Collection<?> c) | It returns true if the list contains all the specified element |
| int indexOf(Object o) | It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element. |
| E remove(int index) | It is used to remove the element present at the specified position in the list. |
| boolean remove(Object o) | It is used to remove the first occurrence of the specified element. |
| boolean removeAll(Collection<?> c) | It is used to remove all the elements from the list. |
| void replaceAll(UnaryOperator<E> operator) | It is used to replace all the elements from the list with the specified element. |
| void retainAll(Collection<?> c) | It is used to retain all the elements in the list that are present in the specified collection. |
| E set(int index, E element) | It is used to replace the specified element in the list, present at the specified position. |
| void sort(Comparator<? super E> c) | It is used to sort the elements of the list on the basis of specified comparator. |
| Spliterator<E> spliterator() | It is used to create spliterator over the elements in a list. |
| List<E> subList(int fromIndex, int toIndex) | It is used to fetch all the elements lies within the given range. |
| int size() | It is used to return the number of elements present in the list. |
The ArrayList and LinkedList classes provide the implementation of the List interface, allowing us to create lists of different data types.
The following example demonstrates how to create List objects using ArrayList and LinkedList with different data types.
In short, we can create a List of any data type. The ArrayList
Here are some essential operations that can be performed on Java Lists.
We can add elements to a List using the add() method. It allows adding elements at the end or at a specific index.
Example:
Elements can be removed from a List either by specifying the object or by providing the index position.
Example:
We can access elements using index-based methods like get() and also find positions using indexOf().
Example:
We can iterate through the elements of a List using loops such as the enhanced for loop.
Example:
Let's see a simple example of List where we are using the ArrayList class as the implementation.
Output:
Mango Apple Banana Grapes
We can convert an array to a List by adding elements one by one using the add() method or by using built-in methods like Arrays.asList() and the ArrayList constructor.
The following example demonstrates how to convert an array into a List by traversing each element.
Output:
Printing Array: [Java, Python, PHP, C++] Printing List: [Java, Python, PHP, C++]
We can convert a List to an array using the toArray() method provided by the List interface.
The following example demonstrates how to convert a List into an array.
Output:
Printing Array: [Mango, Banana, Apple, Strawberry] Printing List: [Mango, Banana, Apple, Strawberry]
The get() method is used to retrieve an element at a specific index, whereas the set() method is used to replace an element at a given index.
The following example demonstrates how to access and update elements in a List.
Output:
Returning element: Apple Mango Dates Banana Grapes
We can sort a List using the Collections.sort() method or the List.sort() method introduced in Java 8.
The following example demonstrates how to sort a List of strings and numbers.
Output:
Apple Banana Grapes Mango Sorting numbers... 1 11 21 51
The ListIterator interface is used to traverse the elements of a List in both forward and backward directions.
It provides additional methods to modify the elements during iteration, such as adding, removing, and updating elements.
Syntax to declare ListIterator Interface is as follows:
The following table contains the commonly used methods of the ListIterator interface along with their descriptions.
| Method | Description |
|---|---|
| void add(E e) | The method inserts the specified element into the list. |
| boolean hasNext() | The method returns true if the list iterator has more elements while traversing the list in the forward direction. |
| E next() | The method returns the next element in the list and advances the cursor position. |
| int nextIndex() | The method returns the index of the element that would be returned by a subsequent call to next() |
| boolean hasPrevious() | The method returns true if this list iterator has more elements while traversing the list in the reverse direction. |
| E previous() | The method returns the previous element in the list and moves the cursor position backward. |
| E previousIndex() | The method returns the index of the element that would be returned by a subsequent call to previous(). |
| void remove() | The method removes the last element from the list that was returned by next() or previous() methods |
| void set(E e) | The method replaces the last element returned by next() or previous() methods with the specified element. |
The following example demonstrates how to use the ListIterator interface to traverse elements in both forward and backward directions.
Output:
Traversing elements in forward direction index:0 value:Amit index:1 value:Sachin index:2 value:Vijay index:3 value:Kumar Traversing elements in backward direction index:3 value:Kumar index:2 value:Vijay index:1 value:Sachin index:0 value:Amit
The following example demonstrates how to use the List interface to store and manage custom objects (Book) in Java.
Output:
101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications and Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6
We request you to subscribe our newsletter for upcoming updates.