List Interface In Java
List Interface
In Java, list interface is the sub interface of collection and declares the behavior of a collection that stores a sequence of elements. It contains methods to insert and delete the elements on index basis. A list may contain duplicate elements. It is a factory of ListIterator interface.
Some of the list methods will throw an UnsupportedOperationException, when the collection cannot be modified and a ClassCastException is generated, when one object is incompatible with another.
Methods of List Interface
void add(int index, Object obj)
This method is used to insert an object into the calling list at the index passed in an index.
boolean addAll(int index, Collection c)
This method is used to insert all the elements of c into the calling list at the index passed in an index. Any pre-existing elements at or beyond the point of insertion are shifted up.
Object get(int index)
This method is used to return the object stored at the particular index within the calling collection.
int indexOf(Object obj)
This method is used to return the index of the first instance of an object in the calling list. If an object is not an element of the list, .1 is returned.
int lastIndexOf(Object obj)
This method is used to return the index of the last instance of an object in the calling list. If an object is not an element of the list, .1 is returned.
ListIterator listIterator( )
This method is used to return an iterator to the start of the calling list.
ListIterator listIterator(int index)
This method is used to return an iterator to the calling list, which begins at the particular index.
Object remove(int index)
This method is used to remove the element at the position index from the calling list and returns the deleted element.
Object set(int index, Object obj)
This method is used to assign an object to the location, specified by an index within the calling list.
List subList(int start, int end)
This method is used to return a list, which includes the elements from start to end in the calling list. Elements in the returned list are also referenced by the calling object.
ListIterator Interface
ListIterator Interface is mainly used to traverse the element in backward and forward direction in Java.
Methods of ListIterator Interface
public boolean hasNext();
public Object next();
public boolean hasPrevious();
public Object previous();
Let’s see an example of ListIterator Interface, given below.
Code
- import java.util.*;
- public class CollectionsExample {
- public static void main(String args[]) {
- ArrayList < String > a = new ArrayList < String > ();
- a.add("James");
- a.add("Mia");
- a.add("Marria");
- a.add(1, "John");
- System.out.println("Second element: " + a.get(2));
- ListIterator < String > i = a.listIterator();
- System.out.println("In forward direction...");
- while (i.hasNext()) {
- System.out.println(i.next());
- }
- System.out.println("In backward direction...");
- while (i.hasPrevious()) {
- System.out.println(i.previous());
- }
- }
- }
Output
Summary
Thus, we learnt that a list interface is the sub interface of collection and declares the behavior of a collection, which stores a sequence of elements. It contains the methods to insert and delete the elements in index basis. A list may contain duplicate elements and also learnt its important methods in Java.