Enroll in Selenium Training

In Java, a List is an ordered collection. The interface provides (CRUD) methods to Create, Read, Update and Delete elements based on their indices. In the context of this article, whenever we refer to List interface - or List for short, it is the built-in List implementation of the Java language. We also call it a List collection since it inherits the Collection interface. And a List is a short name for an object of any class implementing the List interface.

The Collection interface requires iterator, add, remove, equals, and hashCode methods. So, any class that implements the List interface has to implement List-specific methods as well as all these Collection's methods.

By default, the List collection allow duplicate items within a List. Like Array, a List has its index starting at 0.

Methods provided by List interface :

Method: add()

Methods to insert elements at an arbitrary position in a List.

// Appends the specified element to the end of this list.
boolean add(E e);


// Inserts the specified element at the specified position in this list.
void   add(int index, E element);

Method: remove()

Methods to remove elements at an arbitrary position in a List.

// Removes the element at the specified position in this list.
E   remove(int index);

// Removes the first occurrence of the specified element from this list, if it is present.
boolean remove(Object o);

Method: get()

One method for positional access to list elements. Although method invocations are the same, the operations can take constant time, such as in ArrayList, or linear time proportional to the index value, such as in LinkedList.

// Access the element at the specified position in this list.
E   get(int index);

Method: iterator()

A special iterator called ListIterator, which allows bidirectional traversal of a List's elements. It is different from Iterator which can only move to the next element using next(). For the reader unfamiliar with Iterator in Java, you can think of an Iterator as an object that points to a page of a book. Whenever it's next() method is invoked, it returns the current page and point to the next page.

// Get a list iterator over the elements in the list.
ListIterator<E> listIterator();

// Get a list iterator over the elements in the list,
// which starts at the specified position in the list.
ListIterator<E> listIterator(int index);

Method: indexOf() , lastIndexOf() & contains()

Two methods to find the position of a specified element. The operation may perform linear searches, such as in LinkedList or binary searches, such as in ArrayList.

// Find the index of the first occurrence of the specified element in this list,
// or -1 if this list does not contain the element.
int indexOf(Object o);

// Find the index of the last occurrence of the specified element in this list,
// or -1 if this list does not contain the element.
int lastIndexOf(Object o);

// Determine whether this list contains the specified element.
boolean contains(Object o);

Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback