list interface

The Java List Interface, Implementations and Examples

List interface is an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List interface generally has precise control over where in the each list element is inserted and can access elements by their integer index (position).

Additional Operations Supported by “List” Interface over “Collection”

The additional operations supported by List interface over Collection interface are:

  • Positional access manipulates elements based on their numerical position in the list.
  • Search searches for a specified object in the list and returns its numerical position.
  • Iteration extends Iterator semantics to take advantage of the sequential nature of the list.
  • Range-view performs arbitrary range operations on the list.

Implementations of List Interface

ArrayList: 

ArrayList is resizable-array implementation of the List interface. It implements all optional list operations and permits all elements and even allows NULL. And it is more or less equal to Vector, except that it is not synchronized. To prevent accidental unsynchronized access to the ArrayList you need to do the following during creation time.

Here are some highlights about ArrayList:

  • Offers constant-time positional access
  • Gives you fast iteration and fast random access
  • It is an ordered collection (by index), but not sorted.
  • Most commonly used implementation.

LinkedList:

Linked list is a linear collection of data elements. It is a data structure consisting collection of nodes represented in sequence. Each nodes contains two members: data and reference. Reference is nothing but a link to the next node in the sequence.

This data structure allows to efficiently insert or remove elements from any position in the sequence during iteration.

Here are some highlights about Linked List:

  • The access time is linear.
  • Faster access, such as random access is not feasible.
  • The principal benefit of linked list over a conventional array is that they are dynamic i.e., the list elements can be easily inserted or removed without reallocation or reorganization of entire structure.

Example : List Interface and ArrayList

Output

ListIterator Interface

ListIterator interface is an iterator for lists that allows to traverse the list in either direction. A list iterator has no current element and its cursor position always lies between the elements. So an iterator for a list of length n has n+1 possible cursor positions as shown below.

Example: LinkedList

Output

Related Posts

Reference

Leave a Reply

avatar
  Subscribe  
Notify of