Declaration of Java List Interface
public interface List<E> extends Collection<E> ;
Let us elaborate on creating objects or instances in a List class. Since List is an interface, objects cannot be created of the type list. We always need a class that implements this List in order to create an object. And also, after the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the List. Just like several other user-defined ‘interfaces’ implemented by user-defined ‘classes’, List is an ‘interface’, implemented by the ArrayList class, pre-defined in java.util package.
Syntax of Java List
This type of safelist can be defined as:
List<Obj> list = new ArrayList<Obj> ();
Note: Obj is the type of the object to be stored in List
List Interface in Java with Examples
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.
Table of Content
- List Interface in Java
- Declaration of Java List Interface
- Example of Java List
- Operations in a Java List Interface
- Complexity of List Interface in Java
- Iterating over List Interface in Java
- Methods of the List Interface
- Java List vs Set