The List interface in Java is a part of the Java Collections Framework, which provides a way to store and manipulate a group of objects. A list is an ordered collection of elements, and it allows duplicate elements as well. In this article, we will discuss the List interface in Java in detail, along with some examples.
Syntax of List Interface:
The syntax of the List interface is as follows:
public interface List extends Collection
Here, E represents the type of elements that the list will contain. The List interface extends the Collection interface, which means that all the methods of the Collection interface are available in the List interface.
Common Methods of List Interface:
The List interface provides several methods for adding, removing, and accessing elements in a list. Some of the common methods are:
- add(E e) – Adds the specified element to the end of the list.
- add(int index, E element) – Inserts the specified element at the specified position in the list.
- remove(Object o) – Removes the first occurrence of the specified element from the list, if it is present.
- remove(int index) – Removes the element at the specified position in the list.
- get(int index) – Returns the element at the specified position in the list.
- set(int index, E element) – Replaces the element at the specified position in the list with the specified element.
- indexOf(Object o) – Returns the index of the first occurrence of the specified element in the list, or -1 if the element is not present.
- size() – Returns the number of elements in the list.
- Examples:
Let’s see some examples to understand how the List interface works.
Example 1: Adding Elements to a List
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List names = new ArrayList();
names.add("John");
names.add("Mary");
names.add("Peter");
System.out.println(names);
}
}
Output:
[John, Mary, Peter]
In the above example, we have created an ArrayList named names and added three elements to it using the add() method. Finally, we have printed the names ArrayList using the println() method.
Example 2: Removing Elements from a List
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List names = new ArrayList();
names.add("John");
names.add("Mary");
names.add("Peter");
names.remove("Mary");
System.out.println(names);
}
}
Output:
[John, Peter]
In the above example, we have created an ArrayList named names and added three elements to it using the add() method. We have then removed the element “Mary” from the list using the remove() method. Finally, we have printed the names ArrayList using the println() method.
Example 3: Accessing Elements in a List
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List names = new ArrayList();
names.add("John");
names.add("Mary");
names.add("Peter");
String name = names.get(1);
System.out.println(name);
}
}
Output:
Mary
In the above example, we have created an ArrayList named names and added three elements to it using the add() method. We have
then accessed the second element in the list using the get() method and stored it in a variable named name. Finally, we have printed the name variable using the println() method.
Example 4: Replacing Elements in a List
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List names = new ArrayList();
names.add("John");
names.add("Mary");
names.add("Peter");
names.set(1, "Jane");
System.out.println(names);
}
}
Output:
[John, Jane, Peter]
In the above example, we have created an ArrayList named names and added three elements to it using the add() method. We have then replaced the second element “Mary” with “Jane” using the set() method. Finally, we have printed the names ArrayList using the println() method.
Conclusion:
The List interface in Java provides a way to store and manipulate a group of elements in an ordered manner. It allows duplicate elements and provides several methods for adding, removing, and accessing elements in the list. The List interface is implemented by several classes in the Java Collections Framework, such as ArrayList, LinkedList, and Vector. Understanding the List interface is essential for working with collections in Java, and it is a fundamental concept in programming.