CodingBison

A list interface is one of the widely used Collection interfaces. List interface as shown in the below figure extends Collection interface, and hence it inherits all the operations from Collection interface.



Figure: List Interface and Class Hierarchy

How different is List compared to other Collections ? Here is the answer. First, List is an ordered Collection (aka sequence), which means the List stores all the objects in the same order in which they are added. Second, a List allows duplicate entries, this means an object say, obj1 can be added n times to the list and the List has "n" instances of the same object obj1.

As shown in the above figure, there are two classes (ArrayList and LinkedList) that implement the interface List. We will see each of those classes in detail going further.

Methods defined in List interface

Things To Remember
List is an ordered Collection and it allows duplicate elements. All List objects can be accessed using an index.

As we know List is an ordered Collection; all the objects in the List can be accessed/manipulated using an integer index, which represents the position at which they are stored in the List. Just like a standard array, the index starts at 0. List interface has defined new methods that helps in accessing/manipulating the objects using an Index. List interface inherits all methods from the Collection interface, in addition to defining its own.

The below table represents only the methods that are defined by the List interface. You can imagine this table to be an extension of the table Collection Interface Methods list.


Table: Methods defined in List Interface
MethodDescription
void add(int i, Object o)Adds an object "o" at the specified index i, to the List that invokes this method.
bool addAll(int i, Collection c)Inserts all the objects of List "c" at the specified index i, to the List that invokes this method. Returns True on Success, False on Failure.
Object get(int i)Returns the object at the index i, from the List that invokes this method.
Object set(int i, Object o)Replaces the object at the index i, from the List that invokes this method. Returns the element that got replaced.
Object remove(int i)Removes the object at index i, from the List that invokes this method. Returns the removed object. Note: The list is adjusted and made sure the insertion order is maintained.
int indexOf(Object o)Returns the index of first occurrence of the object "p", from the List that invokes this method.
int lastIndexOf(Object o)Returns the index of last occurrence of the object "p", from the List that invokes this method.
ListIterator listIterator()Returns an iterator over the List that invokes this method. The returned iterator object is used to iterate over all the objects present in the List.
ListIterator listIterator(int index)Returns an iterator over the List that invokes this method. The returned iterator would point to the index specified in the parameter "index". The returned iterator object is used to iterate over all the objects present in the List.

Here is an Example program that demonstrates the usage of all the above mentioned operations. The example have used ArrayList and demonstrated the usage of the methods defined by the List interface. As shown in the Figure at the top of this page, ArrayList is one of the Classes that implements the List interface.

 import java.util.ArrayList;

 public class ArrayListDemo {
    public static void main(String[] args) {
        /* If a collection object is declared this way, then there 
         * is no need for an explicit type casting, while adding an
         * object to this collection. In the Below example, notice 
         * the difference in the 2 statements.
         * alistObj1.add(1);  Vs  alistObj2.add(new Integer(10));
 	*/
 	ArrayList<Integer> alistObj1 = new ArrayList<Integer>();
 	alistObj1.add(1);
 	alistObj1.add(33);
 	ArrayList alistObj2= new ArrayList();
 	alistObj2.addAll(alistObj1);
 	alistObj2.add(new Integer(10));

 	alistObj2.add(1, new Integer(12));
 	// Prints the Entire List, after adding 12 at index 1
 	System.out.println(" Printing the list: " + alistObj2);

 	/* Print the object, at index 1 from the list */
 	Object obj = alistObj2.get(1);
 	System.out.println(" Object at Index 1: " + obj);

 	/* Replace the value at index 2 */
 	Object oldValue = alistObj2.set(2,25);
 	System.out.print(" Old Value at Index 2: " + oldValue);
 	System.out.println( ", Replaced Value: " + alistObj2.get(2));

 	// Prints the Entire List, after adding 12 at index 1
 	System.out.println(" Printing the list: " + alistObj2);

 	// Remove the object at index 2
 	alistObj2.remove(2);

 	// Prints the Entire List, after adding 12 at index 1
 	System.out.println(" List after removal of object at index 2: " + alistObj2);

 	// Get the index of the Object
 	System.out.println(" Object 10's index in the List is: " + alistObj2.indexOf(10));
    }
 }

Let us Compile/run the program, Here is the output:

  Printing the list: [1, 12, 33, 10]
  Object at Index 1: 12
  Old Value at Index 2: 33, Replaced Value: 25
  Printing the list: [1, 12, 25, 10]
  List after removal of object at index 2: [1, 12, 10]
  Object 10's index in the List is: 2

View Operations on the List

List interface provides few powerful view operations/methods, where one can get to view the sub-set of a list without having to actually create/form one out of the original list. The method "List<object> subList(int fromIndex, int toIndex)" returns a List, which is just a view of the original List. What does view mean here ?

So, the returned list is called a view, because it is backed by the original list, which means any changes to one of the Lists will be visible/propagated to the other List. It is always recommended to use the sub-list for a short duration of time, and the behavior could be not as expected.

Here is an example that demonstrate the usage of the view operation.

 import java.util.ArrayList;
 import java.util.List;

 public class ListViewOperationsDemo {
     public static void main(String[] args) {
 	ArrayList<Integer> alistObj = new ArrayList<Integer>();
 	alistObj.add(13);
 	alistObj.add(17);
 	alistObj.add(7);
 	alistObj.add(42);
 	alistObj.add(136);

 	System.out.println( "Full Original List : " + alistObj);
 	List<Integer> asubListObj = alistObj.subList(2, 4);
 	System.out.println( "Sub List : " + asubListObj);
 	asubListObj.add(44);

 	/* Adds an element to the end of the Sublist */
 	System.out.println( "Sub List After Addition of new Element : "
 	                    + asubListObj);

 	/* The Above addition has the ripple effect on 
 	 * the original list as well.
 	 */
 	System.out.println( "Modified Full List : " + alistObj);
     }
 }

Let us Compile/run the program, Here is the output:

 Full Original List : [13, 17, 7, 42, 136]
 Sub List : [7, 42]
 Sub List After Addition of new Element : [7, 42, 44]
 Modified Full List : [13, 17, 7, 42, 44, 136]




comments powered by Disqus