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.
- Maintains insertion order
- Allows duplicate elements
- Supports null elements (implementation dependent)
- Supports bidirectional traversal using ListIterator
Declaration of Java List Interface
public interface List<E> extends Collection<E> { }
To use a List, we must instantiate a class that implements it. Example classes that implement it are ArrayList and LinkedList
List<Type> list = new ArrayList<Type>();
import java.util.*;
class Geeks {
public static void main(String[] args)
{
// Creating a List of Strings using ArrayList
List<String> li = new ArrayList<>();
// Adding elements in List
li.add("Java");
li.add("Python");
li.add("DSA");
li.add("C++");
System.out.println("Elements of List are:");
// Iterating through the list
for (String s : li) {
System.out.println(s);
}
}
}
Output
Elements of List are: Java Python DSA C++