![]() |
VOOZH | about |
The List interface in Java extends the Collection interface and is part of the java.util package. It is used to store ordered collections where duplicates are allowed and elements can be accessed by their index.
List<Type> list = new ArrayList<Type>();
public interface List<E> extends Collection<E> { }
Elements of List are: Java Python DSA C++
Explanation: This Java program demonstrates how to create a List using ArrayList, add elements to it, and iterate through the list to print each element. It uses an enhanced for loop to display all the stored programming languages.
It extends the Collection interface.
The common implementation classes of the List interface are:
List can be used only with a class that implements this interface. Now, let's see how to perform a few frequently used operations on the List.
To add an element to the list, we can use the add()method. This method is overloaded to perform multiple operations based on different parameters.
[Geeks, For, Geeks]
Note: If we try to add element at index 1 before adding elements at index 0 it will throw an error. It is always recommended to add elements in a particular index only when the size is defined or to add them sequentially.
To update an element in a list, use the set() method with the target index and the new value. Since List is indexed, the element is replaced at the specified position.
Initial ArrayList [Geeks, Geeks, Geeks] Updated ArrayList [Geeks, For, Geeks]
Searching in a List can be done using indexOf(), lastIndexOf() methods.
First Occurrence of 2 is at Index: 1 Last Occurrence of 2 is at Index: 3
To remove an element from a list, we can use the remove() method. This method is overloaded to perform multiple operations based on different parameters.
Initial ArrayList [Geeks, For, Geeks] After the Index Removal [Geeks, Geeks] After the Object Removal [Geeks]
To access an element in the list, we can use the get() method, which returns the element at the specified index.
get(int index): This method returns the element at the specified index in the list.
Geeks For Geeks [Geeks, For, Geeks]
To check if an element is present in the list, we can use the contains() method. This method returns true if the specified element is present in the list, otherwise, it returns false.
contains(Object o): This method takes a single parameter, the object to be checked if it is present in the list.
Is Geeks present in the list? true
For larger datasets, Lists can be iterated using:
These methods allow efficient traversal of elements.
Geeks For Geeks Geeks For Geeks
Operation | Time Complexity | Space Complexity |
|---|---|---|
Adding Element in List Interface | O(1) | O(1) |
Remove Element from List Interface | O(N) | O(1) |
Replace Element in List Interface | O(1) | O(1) |
Traversing List Interface | O(N) | O(1) |
Methods | Description |
|---|---|
| add(int index, element) | This method is used with Java List Interface to add an element at a particular index in the list. When a single parameter is passed, it simply adds the element at the end of the list. |
| addAll(int index, Collection collection) | This method is used with List Interface in Java to add all the elements in the given collection to the list. When a single parameter is passed, it adds all the elements of the given collection at the end of the list. |
| size() | This method is used with Java List Interface to return the size of the list. |
| clear() | This method is used to remove all the elements in the list. However, the reference of the list created is still stored. |
| remove(int index) | This method removes an element from the specified index. It shifts subsequent elements(if any) to left and decreases their indexes by 1. |
| remove(element) | This method is used with Java List Interface to remove the first occurrence of the given element in the list. |
| get(int index) | This method returns elements at the specified index. |
| set(int index, element) | This method replaces elements at a given index with the new element. This function returns the element which was just replaced by a new element. |
| indexOf(element) | This method returns the first occurrence of the given element or -1 if the element is not present in the list. |
| lastIndexOf(element) | This method returns the last occurrence of the given element or -1 if the element is not present in the list. |
| equals(element) | This method is used with Java List Interface to compare the equality of the given element with the elements of the list. |
| hashCode() | This method is used with List Interface in Java to return the hashcode value of the given list. |
| isEmpty() | This method is used with Java List Interface to check if the list is empty or not. It returns true if the list is empty, else false. |
| contains(element) | This method is used with List Interface in Java to check if the list contains the given element or not. It returns true if the list contains the element. |
| containsAll(Collection collection) | This method is used with Java List Interface to check if the list contains all the collection of elements. |
| sort(Comparator comp) | This method is used with List Interface in Java to sort the elements of the list on the basis of the given comparator. |
Both the List interface and the Set interface inherits the Collection interface. However, there exists some differences between them.
List | Set |
|---|---|
| The List is an ordered sequence. | The Set is an unordered sequence. |
| List allows duplicate elements | Set doesn’t allow duplicate elements. |
| Elements by their position can be accessed. | Position access to elements is not allowed. |
| Multiple null elements can be stored. | The null element can store only once. |
| List implementations are ArrayList, LinkedList, Vector, Stack | Set implementations are HashSet, LinkedHashSet. |