Java List Interface

  • List interface in java is ordered collection so it can store elements in sequence.
  • List interface is sub interface and it extends to Collection interface.
  • List interface can hold duplicate elements so you can insert duplicate elements too.
  • You can access elements from list by integer index.
  • Collection framework has many interfaces in java and list interface is one of them.
  • List interface provides ListIterator to traverse through elements list in backward and forward directions.
List interface java


Few mostly used methods in list interface are as bellow.
  • void add(int index,Object element) : It will insert element at specified index position in list.
  • boolean addAll(int index,Collection c) : It will add all elements of given collection in to the list at specified index position.
  • object get(int Index ) : It will return object at specified index position from list.
  • object set(int index,Object element) : It will assign object at specified index position in list.
  • object remove(int index) : It will remove object from specified position.
  • ListIterator listIterator() : It will returns the list iterator at start of invoking list.
  • ListIterator listIterator(int x) : It will returns the list iterator at specified index position in list.
  • int indexOf(Object obj) : It will return index of first instance of object from invoking list.
  • int lastIndexOf(Object obj) : It will return index of last instance of object from invoking list.
List interface is implemented by many classes like Arraylist, Linkedlist and Vector.  Here, I have represented example of list interface using few methods from different class implementation.

package JAVAExamples;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class InterfaceList {

 public static void main(String[] args) {
  // Add elements in ArrayList.
  List al = new ArrayList();
  al.add("New York");
  al.add("Delhi");
  al.add("Tokyo");
  
  // Print arraylist elements.
  System.out.println("ArrayList Elements");
  System.out.print(al);
  System.out.println();

  // Add elements in linkedlist.
  List ll = new LinkedList();
  ll.add("New York");
  ll.add("Delhi");
  ll.add("Tokyo");
  
  // Print linkedlist elements.
  System.out.println();
  System.out.println("LinkedList Elements");
  System.out.print(ll);
  System.out.println();

  // Get and print arraylist's 3rd element.
  System.out.println();
  System.out.println("Element at 3rd position in arralist is : " + al.get(2));

  // Using ListIterator to traverse through forward and backward directions in arralist.
  ListIterator<String> itrtr = al.listIterator();

  // Traverse in forward direction.
  System.out.println();
  System.out.println("Traversing through arralist elements in forward direction...");
  while (itrtr.hasNext()) {
   System.out.println(itrtr.next());
  }

  // Traverse in backward direction.
  System.out.println();
  System.out.println("Traversing through arralist elements in backward direction...");
  while (itrtr.hasPrevious()) {
   System.out.println(itrtr.previous());
  }
 }
}

Output
ArrayList Elements
[New York, Delhi, Tokyo]

LinkedList Elements
[New York, Delhi, Tokyo]

Element at 3rd position in arralist is : Tokyo

Traversing through arralist elements in forward direction...
New York
Delhi
Tokyo

Traversing through arralist elements in backward direction...
Tokyo
Delhi
New York

In above example you can see how arralist, linkedlist, get method and listiterator works.

2 comments:

  1. I love all of your examples. typical and easy to understand.

    ReplyDelete
  2. then what is the difference between arraylist and linkedlist?

    ReplyDelete