List: 

In Java, a list is a collection that represents an ordered sequence of elements. It allows you to store and manipulate a group of objects. 

The List interface is part of the Java Collections Framework and provides various methods to add, remove, retrieve, and manipulate elements in the list.


Here are some key features of a List collection in Java:


Ordered Elements: Lists maintain the order of elements as they are inserted. Each element has an associated index that represents its position in the list.


Duplicates: Lists can contain duplicate elements. Unlike sets, which enforce uniqueness, lists allow multiple occurrences of the same element.


Dynamic Size: Lists can dynamically grow or shrink as elements are added or removed. You don't need to specify an initial size for a list.


Index-based Access: Elements in a list can be accessed by their index. The index starts from 0 for the first element and goes up to size() - 1 for the last element.


Common Operations: Lists provide various methods to add, remove, and retrieve elements. Some common operations include add(element), remove(index), get(index), size(), contains(element), and more.


Some of the commonly used implementations of the List interface are:


ArrayList: ArrayList is a resizable array-backed implementation of a list. It provides fast random access and is efficient for element retrieval by index. 

However, it is not suitable for frequent insertions or deletions in the middle of the list.


LinkedList: LinkedList is a doubly linked list implementation of a list. It provides efficient insertions and deletions at both ends of the list. 

It is suitable for scenarios where frequent modifications (insertions/deletions) are required. However, random access to elements is slower compared to ArrayList.


Vector: Vector is similar to ArrayList but is synchronized, making it thread-safe for concurrent access. It provides the same functionalities as ArrayList but with the added synchronization overhead.


Stack: Stack is a subclass of Vector that implements the LIFO (Last-In-First-Out) stack data structure. It provides stack-specific operations like push() and pop(), in addition to the regular list operations.


CopyOnWriteArrayList: CopyOnWriteArrayList is a thread-safe variant of ArrayList where all write operations make a fresh copy of the underlying array.

 It is suitable for scenarios where the list is frequently read but rarely modified.

List Collection Methods:

In the Java Collections Framework, the List interface provides a variety of methods to work with lists. Here are some commonly used methods available in the List interface:


Adding Elements:


boolean add(E element): Adds the specified element to the end of the list.

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

boolean addAll(Collection<? extends E> collection): Adds all elements from the specified collection to the end of the list.

boolean addAll(int index, Collection<? extends E> collection): Inserts all elements from the specified collection into the list at the specified position.

Accessing Elements:


E get(int index): Returns the element at the specified index in the list.

int indexOf(Object element): Returns the index of the first occurrence of the specified element in the list.

int lastIndexOf(Object element): Returns the index of the last occurrence of the specified element in the list.

List<E> subList(int fromIndex, int toIndex): Returns a new list containing elements from the specified range.

Removing Elements:


E remove(int index): Removes the element at the specified index from the list and returns it.

boolean remove(Object element): Removes the first occurrence of the specified element from the list.

boolean removeAll(Collection<?> collection): Removes all elements in the specified collection from the list.

void clear(): Removes all elements from the list.

Updating Elements:


E set(int index, E element): Replaces the element at the specified index with the specified element and returns the original element.

Checking List Size and Contents:


int size(): Returns the number of elements in the list.

boolean isEmpty(): Returns true if the list is empty.

boolean contains(Object element): Returns true if the list contains the specified element.

boolean containsAll(Collection<?> collection): Returns true if the list contains all elements in the specified collection.

Iterating Over the List:


Iterator<E> iterator(): Returns an iterator to iterate over the elements in the list.

ListIterator<E> listIterator(): Returns a list iterator to iterate over the elements in the list.

These are just some of the commonly used methods available in the List interface. The List interface also extends the Collection interface, so it inherits additional methods from there


What is ArrayList?

How to create ArrayList?

How many ways to Iterate ArrayList?