List Interface
List Interface
The List interface in Java provides a way to store the ordered collection. It
is a child interface of Collection. It is an ordered collection of objects in
which duplicate values can be stored. Since List preserves the insertion
order, it allows positional access and insertion of elements.
The List interface is found in java.util package and inherits the Collection
interface. It is a factory of the ListIterator interface. Through the
ListIterator, we can iterate the list in forward and backward directions.
The implementation classes of the List interface are ArrayList, LinkedList,
Stack, and Vector. ArrayList and LinkedList are widely used in Java
programming. The Vector class is deprecated since Java 5.
2. Removing Elements
In order 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. They are:
Parameters:
• remove(Object): This method is used to simply remove an
object from the List. If there are multiple such objects, then the
first occurrence of the object is removed.
• remove(int index): Since a List is indexed, this method takes an
integer value which simply removes the element present at that
specific index in the List. After removing the element, all the
elements are moved to the left to fill the space and the indices
of the objects are updated.
Example:
// Java Program to Remove Elements from a List
// Main class
class GFG {
}
}
3. Updating elements
After adding the elements, if we wish to change the element, it can be
done using the set() method. Since List is indexed, the element which we
wish to change is referenced by the index of the element. Therefore, this
method takes an index and the updated element which needs to be
inserted at that index.
Example:
// Java Program to Update Elements in a List
// Main class
class GFG {
5. Accessing Elements
In order to access an element in the list, we can use the get() method,
which returns the element at the specified index
Parameters:
get(int index): This method returns the element at the specified index in
the list.
Example:
// Java Program to Access Elements of a List
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an object of List interface,
// implemented by ArrayList class
List<String> al = new ArrayList<>();
// Adding elements to object of List interface
al.add("Geeks");
al.add("For");
al.add("Geeks");
Example:
// Java Program to Check if an Element is Present in a List
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an object of List interface,
// implemented by ArrayList class
List<String> al = new ArrayList<>();
7. addAll()
This method appends all of the elements in the specified collection to the
end of this list, in the order that they are returned by the specified
collection’s iterator.
// Driver code
public static void main(String[] args)
{
// Initializing a list of type arraylist
List<Integer> l = new ArrayList<>();
l.add(10);
l.add(15);
l.add(20);
System.out.println(l);
l.addAll(arr);
System.out.println(l);
}
}
8. size() method
The size() method of the List interface in Java is used to get the number
of elements in this list. That is, the list size() method returns the count of
elements present in this list container.
// Java program to Illustrate size() method
// of List class for Integer value
// Main class
public class GFG {
9. clear() method
The clear() method of List interface in Java is used to remove all of the
elements from the List container. This method does not deleted the List
container, instead it just removes all of the elements from the List.
Example
// Java code to illustrate clear() method
import java.io.*;
import java.util.*;
// Main class
public class GFG {