Java – List interface

In this article, we will discuss List interface with all its important methods in detail

1. Key point about List:

  • allows duplicate items
  • maintains insertion order

2. List interface:

  • List is a sub-interface of Collection interface (i.e.; List extends Collection)
  • To represent a group of element/objects as a single unit/entity, which allows to add duplicate element/objects as well as maintains insertion-order, then programmer should consider using List
  • So, List allows duplicates element/objects maintaining insertion-order
  • There are 3 concrete classes for List interface viz.; ArrayList, LinkedList and Vector
  • List interface defines various list specific methods; in addition to inherited Collection methods
  • These specific methods can be used to operate on list objects only
  • The biggest advantage of using List specific methods is, we can insert/access element/objects of List by their position using zero-based index
  • Present in java.util package and extends java.util.Collection interface
3-List-interace-in-java

Source: Team BenchResources.Net

3. List interface methods:

List methods Description
void add(int index, Object obj);add/inserts a single element/object to invoking collection at the specified index
boolean addAll(int index, Collection c);add/inserts group of element/objects to invoking collection at the specified index

 

(i.e.; adding specified collection to invoking collection at the specified index)

Object get(int index);retrieve element/object from the invoking collection at the specified index
Object remove(int index);remove/deletes a single element/object from invoking collection at the specified index
Object set(int index, Object newObj);replaces old object from invoking collection with new object passed at the specified index

 

returns old object;

List subList(int start, int end)returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive)

 

Note: If fromIndex and toIndex are equal, the returned list is empty

int indexOf(Object obj);returns first occurrence of specified element/object in the invoking collection

 

returns -1, if list doesn’t contain the element

int lastIndexOf(Object obj);returns last occurrence of specified element/object in the invoking collection

 

returns -1, if list doesn’t contain the element

ListIterator listIterator; returns listIterator for the invoking collection; to iterate over the collection elements in both FORWARD & BACKWARD directions

Note : All above listed List methods will be inherited to subsequent List implemented classes

4. Below listed classes implements List interface

  • ArrayList
  • LinkedList
  • Vector

5. Factors to consider while discussing any collection class

We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,

  • Underlying data structure
  • Duplicates are allowed or Not
  • Insertion order is maintained or Not
  • Whether NULL insertion is possible or Not
  • If possible, how many NULL values can be inserted
  • Whether collection class provide sorting, by default
  • Is there any way to apply customized sorting
  • Performance, while dealing with retrieval or manipulation (addition/deletion)
  • By default, all methods are synchronized or Not

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - ArrayList class with example
Java - Collection interface