Java Methods of LinkedList
In Java, the LinkedList
class provides several methods for adding, removing, and accessing elements in the list. Here are some of the key methods defined in the LinkedList
class:
void add(E element)
: Adds the specified element to the end of the list.void addFirst(E element)
: Inserts the specified element at the beginning of the list.void addLast(E element)
: Adds the specified element to the end of the list.boolean addAll(Collection<? extends E> c)
: Adds all the elements in the specified collection to the end of the list.boolean addAll(int index, Collection<? extends E> c)
: Inserts all the elements in the specified collection at the specified position in the list.E remove()
: Removes and returns the first element of the list.E removeFirst()
: Removes and returns the first element of the list.E removeLast()
: Removes and returns the last element of the list.boolean remove(Object o)
: Removes the first occurrence of the specified element from the list.E get(int index)
: Returns the element at the specified position in the list.E set(int index, E element)
: Replaces the element at the specified position in the list with the specified element.int size()
: Returns the number of elements in the list.boolean contains(Object o)
: Returnstrue
if the list contains the specified element.void clear()
: Removes all the elements from the list.boolean isEmpty()
: Returnstrue
if the list is empty.int indexOf(Object o)
: Returns the index of the first occurrence of the specified element in the list, or-1
if the element is not found.int lastIndexOf(Object o)
: Returns the index of the last occurrence of the specified element in the list, or-1
if the element is not found.Iterator<E> iterator()
: Returns an iterator over the elements in the list.ListIterator<E> listIterator()
: Returns a list iterator over the elements in the list.ListIterator<E> listIterator(int index)
: Returns a list iterator over the elements in the list, starting at the specified position.
The LinkedList
class also implements the Deque
interface, which provides additional methods for adding and removing elements from both ends of the list.