List interface With Example

A List is an ordered Collection (sometimes called a sequence) meaning you can access the elements of a List in a specific order, and by an index too. You can also add the same element more than once to a List. Lists may contain duplicate elements. List Interface which is in java.util package is a subtype of java.util.Collection Interface.

The list interface includes following operations:

  • Positional access: manipulates elements based on their numerical position in the list. This includes methods such as get, set, add, addAll, and remove.
  • Search: Search operation returns the numerical position of the object in the list. Search methods include indexOf and lastIndexOf.
  • Iteration: Extends Iterator semantics to get advantage of the list’s sequential nature. The listIterator methods provide this behavior.
  • Range-view: The sublist method performs arbitrary range operations on the list.

Concrete Implementation of List Interface:

List is an interface you need to instantiate a concrete implementation of the interface in order to use it. Following List implementations in the Java Collections API.

  • util.ArrayList
  • util.LinkedList
  • util.Vector
  • util.Stack

Here are a few examples of how to create a List instance:

  • List list = new ArrayList()
  • List listB = new LinkedList();
  • List listC = new Vector();
  • List listD = new Stack();

Basic operation with List:

  • Adding element:

To add elements in a list we can use add (Object), add (index, Object) and add All() methods. This method is inherited from the Collection interface. Here are a few examples:

We can add an element into the list at a specified index, for instance:

list.add(0, “Johny”);

  • Accessing Elements :

The order in which the elements are added to the List is stored, so you can access the elements in the same order. The get () method is used to retrieve an element from the list at a specified index. For example:

  • Updating element:

Use the set(index, element) method to replace the element at the specified index by the specified element. For example:

list.set(2, “Depp”);

  • Removing elements :

To remove an element from the list, we can use two below methods.

  • remove(Object element)
  • remove(int index)

remove(Object element): removes that element in the list, if it is present. All subsequent elements in the list are then moved up in the list. Their index thus decreases by 1.

remove(int index) : removes the element at the given index. All subsequent elements in the list are then moved up in the list. Their index thus decreases by 1.If the specified index is out of range (index < 0 or index >= list size), a java.lang.IndexOutOfBoundsException is thrown.

To remove all elements in the list, use the clear() method:

            List.clear();

  • List Size:

You can get the number of elements in the List by calling the size () method. Here is an example

int size = list.size();

  • Iterating a List:

We can iterate Java list in various ways, here we can see two different mechanisms to iterate a Java list.

The first way to iterate a List is to use an Iterator. Here is an example:

Another way to iterate a List is to use the for loop added in Java 5. Here is an example:

Related Posts:

  1. Collections Framework
  2. Collection interface

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons