Introduction

This article explains how the List Interface class and LinkedList class in Java collections work.

List Interface class

It is a part of Collections. It contains methods to insert and delete elements in an index basis. It is a factory of the ListIerator interface.

Some commonly used public methods of the List Interface are:

  1. object set (int index, Object element)
  2. object remove (int index)
  3. ListIterator listIterator()
  4. ListIterator listIterator (int x)
  5. void add (int index, Object element)
  6. boolean addAll (int index, Collection cln)
  7. object get (int Index position)

ListIterator Interface

This interface traverses the list elements in a forward and backward direction.

Some commonly used public methods are:

  1. boolean hasPrevious
  2. Object previous
  3. boolean hasNext()
  4. Object next()
Example
  1. import java.util.*;
  2. class ListInterfaceEx {
  3. public static void main(String args[]) {
  4. ArrayList arylst = new ArrayList();
  5. arylst.add("Jaun");
  6. arylst.add("Shridu");
  7. arylst.add("Ramesh");
  8. arylst.add(1, "Suresh");
  9. System.out.println("At 2nd position the element are: " + arylst.get(2));
  10. ListIterator it = arylst.listIterator();
  11. System.out.println("Print the elements in forward direction...");
  12. while (it.hasNext()) {
  13. System.out.println(it.next());
  14. }
  15. System.out.println("Print the elements in backward direction...");
  16. while (it.hasPrevious()) {
  17. System.out.println(it.previous());
  18. }
  19. }
  20. }
Output
Fig-1.jpg

Introduction to the LinkedList class

It has the following features:
Example
  1. import java.util.*;
  2. class LinkedListEx {
  3. public static void main(String args[]) {
  4. LinkedList arylst = new LinkedList();
  5. arylst.add("Jaun");
  6. arylst.add("Shridu");
  7. arylst.add("Ramesh");
  8. arylst.add(1, "Suresh");
  9. Iterator it = arylst.iterator();
  10. while (it.hasNext()) {
  11. System.out.println(it.next());
  12. }
  13. }
  14. }
Output
Fig-2.jpg