List Interface

0
627

In my last post Collection API, I discussed the basics of Collection Framework and explained about the Collection Interface, few important methods it has and the Collection interface hierarchy. Now in continuation to that in this post, I am going to discuss the List interfaces and the classes which implement List interface.

List Interface

java.util.List implementations are ordered by index position. A position that is determined by setting an object at a specific index or by adding it without specifying position (object is added to the end). It starts with zero index and allows duplicate objects too. List has some of its methods related to index as well Collection interface methods (of-course).

List_Interface_bright

To create the instance of List interface, you have to choose any of the implementing class like ArrayList, LinkedList, Vector or Stack. To show an example :

List booksList = new ArrayList();

Or say,

List booksList = new LinkedList();

Now, let me list down the mentioned classes which are implemented through List interface and throw some light on their features as well.

1) ArrayList

This collection class is the most popular one and most widely used by Java developers. ArrayList is a growable array – which means such dynamic arrays which can grow as needed. Initially the ArrayLists are created with an initial size in the memory. But if this size exceeds, the size of ArrayList would also increase automatically. Few points to keep in mind which make it different than others:

  • ArrayList gives fast iteration and fast random access
  • Starting JDK 1.5, it now implements new ‘RandomAccess’ interface (marker interface) – which says ‘this list supports fast (generally constant time) random access’.
  • Choose ArrayList over a LinkedList when you need ‘fast iteration’ but aren’t doing lot of insertion and deletion.

There are three constructors available for ArrayList :

ArrayList() – Constructs an empty list with an initial capacity of ten.
ArrayList(Collection c) – Constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
ArrayList(int initialCapacity) – Constructs an empty list with the specified initial capacity.

2) LinkedList

In the LinkedList structure, elements are doubly-linked to each other. The main features of it to notice:

  • LinkedList may iterate slowly than ArrayList but is a good choice for ‘fast insertion and deletion’
  • Starting Java 5, it has been enhanced to implement java.util.Queue interface.

LinkedList extends AbstractSequentialList and implements List interface, Cloneable, Serializable. Starting Java 5, it has been enhanced to implement java.util.Queue interface as well and it now supports queue methods like – peek(), poll(), offer().

The LinkedList is not synchronized and it also permits all elements including null. In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue.

LinkedList has two constructors:

LinkedList() – Constructs an empty LinkedList.
LinkedList(Collection c) – Constructs a Linked List containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

3) Vector

You can consider Vector as Synchronized ArrayList. It has the legacy methods which are not in Collection Framework and hence not in ArrayList.
Vector is still used by many of the legacy applications so you need to have a basic understanding of this.

The Vector class implements a growable array of objects and its components can be accessed using an integer index. The size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

There are four constructors of Vector class :

Vector() – Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
Vector(Collection c) – Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
Vector(int initialCapacity) – Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
Vector(int initialCapacity, int capacityIncrement) – Constructs an empty vector with the specified initial capacity and capacity increment.

In the next post, I would discuss Set Interface and its implementation classes. Till then, stay tuned.

References: http://docs.oracle.com/javase/tutorial/collections/interfaces/index.html

Previous articleTop five Java Books
Next articleSet Interface
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY