tutorialcup
List Interface in Java
A Java list or List Interface in Java is a collection or sequence of elements in an ordered manner. Since the list maintains a particular sequence, we can perform any operation like add, delete, get, set on the list using the required index. The index always starts at 0. It extends the Collection interface.
Java List Class or Interface?
The list is an interface and not a class. So we must always say List Interface.
Java List Interface (List Syntax)
We can create a List Interface in the below ways. We can use either ArrayList(), LinkedList() or Stack.
Without object type
We can store any type of value in List if we do not specify the object type.
List listname = new ArrayList();
With object type
We can store only specific value type in List if we specify the object type.
List listname = new ArrayList();
Eg: List listname = new ArrayList(); --> The list can store only String values
Restrict list item count
List listname = new ArrayList(number);
Eg: List listname = new ArrayList(2); --> The list can store only 2 values
Package to import
We need to import the below mentioned package in order to use the Java list interface.
import java.util.List;
Java
List Interface in Java
A Java list or List Interface in Java is a collection or sequence of elements in an ordered manner. Since the list maintains a particular sequence, we can perform any operation like add, delete, get, set on the list using the required index. The index always starts at 0. It extends the Collection interface.
Java List Class or Interface?
The list is an interface and not a class. So we must always say List Interface.
Java List Interface (List Syntax)
We can create a List Interface in the below ways. We can use either ArrayList(), LinkedList() or Stack.
Without object type
We can store any type of value in List if we do not specify the object type.
List listname = new ArrayList();
With object type
We can store only specific value type in List if we specify the object type.
List listname = new ArrayList();
Eg: List listname = new ArrayList(); --> The list can store only String values
Restrict list item count
List listname = new ArrayList(number);
Eg: List listname = new ArrayList(2); --> The list can store only 2 values
Package to import
We need to import the below mentioned package in order to use the Java list interface.
import java.util.List;
Java