Java LinkedList Class Methods

The LinkedList class in Java is a part of the Java Collections Framework and implements the List interface. It provides a linked-list data structure that allows for efficient insertion and removal of elements. The LinkedList is widely used in scenarios where frequent insertions and deletions are required.

This guide covers various methods available in the LinkedList class, offering a comprehensive understanding of how to manipulate and interact with linked lists in Java. These methods are crucial for efficient coding practices and help in performing operations like adding, removing, and accessing elements.

For more detailed information, you can refer to the official Java SE Documentation and additional resources on Java Collections Tutorial.

Method Description
add() Adds an element to the end of the LinkedList.
addAll() Adds all elements from another collection to the LinkedList.
addFirst() Adds an element at the beginning of the LinkedList.
addLast() Adds an element at the end of the LinkedList.
clone() Creates a shallow copy of the LinkedList.
clear() Removes all elements from the LinkedList.
contains() Checks if the LinkedList contains a specified element.
element() Retrieves the first element of the LinkedList without removing it.
descendingIterator() Returns an iterator to iterate over the elements in reverse order.
get() Returns the element at a specified position in the LinkedList.
getFirst() Returns the first element of the LinkedList.
getLast() Returns the last element of the LinkedList.
indexOf() Returns the index of the first occurrence of a specified element.
lastIndexOf() Returns the index of the last occurrence of a specified element.
listIterator() Returns a list iterator to iterate over the elements of the LinkedList.
offer() Adds an element to the end of the LinkedList.
offerFirst() Adds an element at the beginning of the LinkedList.
offerLast() Adds an element at the end of the LinkedList.
peek() Retrieves the first element of the LinkedList without removing it.
peekFirst() Retrieves the first element of the LinkedList without removing it.
peekLast() Retrieves the last element of the LinkedList without removing it.
poll() Retrieves and removes the first element of the LinkedList.
pollFirst() Retrieves and removes the first element of the LinkedList.
pollLast() Retrieves and removes the last element of the LinkedList.
pop() Retrieves and removes the first element of the LinkedList.
push() Adds an element to the front of the LinkedList.
remove() Removes the first occurrence of a specified element from the LinkedList.
removeFirst() Removes the first element of the LinkedList.
removeFirstOccurrence() Removes the first occurrence of a specified element from the LinkedList.
removeLast() Removes the last element of the LinkedList.
removeLastOccurrence() Removes the last occurrence of a specified element from the LinkedList.
set() Replaces the element at a specified position with a new element.
size() Returns the number of elements in the LinkedList.
toArray() Converts the LinkedList to an array.

Leave a Comment

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

Scroll to Top