The java.util.List interface is a subtype of the java.util.Collection interface.
Java Collections Framework- List Interface
The java.util.List interface is a subtype of the java.util.Collection interface. A List is an ordered Collection (sometimes called a sequence). meaning you can access the elements of a List in a specific order, and by an index too. List interface indicates the behavior of the collection of objects. It allows duplicate objects and one or more elements to be null. The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
There are two general-purpose List implementations in the Collections Framework:
Vector-
We can choose between the following List implementations in the Java Collections API:
java.util.ArrayList
java.util.LinkedList
java.util.Vector
java.util.Stack
Here are a few examples of how to create a List instance:
List listA = new ArrayList();
List listB = new LinkedList();
List listC = new Vector();
List listD = new Stack();
In addition to the methods defined by Collection, List defines its own methods, which are as follows-
add (Object o) method of List Interface in java.
add (int index, Object element) method of List Interface in java.
addAll(Collection c) method of List Interface in java.
addAll(int index, Collection c) method of List Interface in java.
clear( ) method of List Interface in java.
contains(Object o) method of List Interface in java.
containsAll(Collection c) method of List Interface in Java
equals(Object o) method of List Interface in Java.
get(int index) method of List Interface in java.
hashCode( ) method of List Interface in java
isEmpty( )method of List Interface in Java
indexOf(Object o) method of List Interface in java
lastIndexOf(Object o) method of List Interface in java
iterator( )method of List Interface in Java.
listIterator ( ) method of List Interface in Java
listIterator (int index) method of List Interface in Java
remove (int index) method of List Interface in java
remove (Object o) method of List Interface in java.
removeAll (Collection c) method of List Interface in java
retainAll (Collection c) method of List Interface in java.
set (int index Object element) method of List Interface in java.
size ( ) method of List Interface in java.
toArray ( ) method of List Interface in java.
subList (int fromIndex,� int toIndex) method of List Interface in java.
toArray (Object [ ] a) method of List Interface in java.
Here is an example that demonstrates a List: ListDemo.java
The interface has been implemented in various classes like ArrayList or LinkedList etc. In this code Iterator is used for reading the objects of the list.
package devmanuals.com; import java.util.*; public class ListDemo { public static void main(String[] args) { List LST = new ArrayList(); LST.add("One"); LST.add("Three"); LST.add("Two"); LST.add("Four"); Iterator it = LST.iterator(); while (it.hasNext()) { String value = (String) it.next(); System.out.println("Value : " + value); } } }
Output:
Value : One
Value : Three Value : Two Value : Four |
[ 0 ] Comments