
Java Linkedlist Methods in Java
LinkedList Methods in Java
In Java, the LinkedList
class is part of the java.util
package and implements both the List
and Deque
interfaces. It provides a variety of methods to manipulate elements in the list. These methods can be categorized based on their functionalities like adding, removing, accessing, and modifying elements.
Here is a comprehensive list of commonly used LinkedList
methods:
Methods for Adding Elements
add(E e)
:Adds the specified element to the end of the list.
list.add("Apple");
addFirst(E e)
:Adds the specified element at the beginning of the list.
list.addFirst("Banana");
addLast(E e)
:Adds the specified element at the end of the list (same as
add
).
list.addLast("Cherry");
offer(E e)
:Adds the element to the end of the list, and returns
true
(it is equivalent toadd
inLinkedList
).
list.offer("Date");
offerFirst(E e)
:Adds the element to the front of the list.
list.offerFirst("Elderberry");
offerLast(E e)
:Adds the element to the end of the list.
list.offerLast("Fig");
Methods for Removing Elements
remove()
:Removes and returns the first element of the list. Throws
NoSuchElementException
if the list is empty.
String removed = list.remove();
removeFirst()
:Removes and returns the first element of the list.
String removedFirst = list.removeFirst();
removeLast()
:Removes and returns the last element of the list.
String removedLast = list.removeLast();
remove(int index)
:Removes the element at the specified position in the list.
list.remove(2); // Removes element at index 2
remove(Object o)
:Removes the first occurrence of the specified element from the list.
list.remove("Apple"); // Removes the first occurrence of "Apple"
poll()
:Removes and returns the first element of the list, or returns
null
if the list is empty.
String polled = list.poll();
pollFirst()
:Removes and returns the first element of the list, or returns
null
if the list is empty.
String polledFirst = list.pollFirst();
pollLast()
:Removes and returns the last element of the list, or returns
null
if the list is empty.
String polledLast = list.pollLast();
clear()
:Removes all elements from the list.
list.clear();
Methods for Accessing Elements
get(int index)
:Returns the element at the specified position in the list.
String element = list.get(1); // Gets the element at index 1
getFirst()
:Returns the first element of the list.
String firstElement = list.getFirst();
getLast()
:Returns the last element of the list.
String lastElement = list.getLast();
peek()
:Returns the first element of the list without removing it, or
null
if the list is empty.
String peeked = list.peek();
peekFirst()
:Returns the first element of the list without removing it, or
null
if the list is empty.
String peekedFirst = list.peekFirst();
peekLast()
:Returns the last element of the list without removing it, or
null
if the list is empty.
String peekedLast = list.peekLast();
Methods for Modifying Elements
set(int index, E element)
:Replaces the element at the specified position with the specified element.
list.set(2, "Grape"); // Sets "Grape" at index 2
addAll(Collection<? extends E> c)
:Appends all elements from the specified collection to the end of the list.
list.addAll(anotherList);
addAll(int index, Collection<? extends E> c)
:Inserts all elements from the specified collection starting at the specified position.
list.addAll(2, anotherList);
removeAll(Collection<?> c)
:Removes all elements from the list that are contained in the specified collection.
list.removeAll(anotherList);
retainAll(Collection<?> c)
:Retains only the elements in the list that are contained in the specified collection.
list.retainAll(anotherList);
Methods for List Information
size()
:Returns the number of elements in the list.
int size = list.size();
isEmpty()
:Returns
true
if the list contains no elements, otherwise returnsfalse
.
boolean empty = list.isEmpty();
contains(Object o)
:Returns
true
if the list contains the specified element.
boolean contains = list.contains("Apple");
indexOf(Object o)
:Returns the index of the first occurrence of the specified element, or
-1
if not found.
int index = list.indexOf("Apple");
lastIndexOf(Object o)
:Returns the index of the last occurrence of the specified element, or
-1
if not found.
int lastIndex = list.lastIndexOf("Apple");
Methods for Iteration
iterator()
:Returns an iterator over the elements in the list.
Iterator<String> iterator = list.iterator();while (iterator.hasNext()) { System.out.println(iterator.next());}
listIterator()
:Returns a list iterator over the elements in the list.
ListIterator<String> listIterator = list.listIterator();while (listIterator.hasNext()) { System.out.println(listIterator.next());}
forEach()
:Performs the given action for each element of the list.
list.forEach(System.out::println);
Example Usage of LinkedList Methods
import java.util.LinkedList;public class LinkedListMethodsExample { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>(); // Adding elements list.add("Apple"); list.addFirst("Banana"); list.addLast("Cherry"); // Accessing elements System.out.println("First Element: " + list.getFirst()); System.out.println("Last Element: " + list.getLast()); // Modifying elements list.set(1, "Blueberry"); // Removing elements list.removeFirst(); list.removeLast(); // Iterating over the list System.out.println("LinkedList Elements:"); list.forEach(System.out::println); // Checking the size System.out.println("Size of list: " + list.size()); }}
Output:
First Element: BananaLast Element: CherryLinkedList Elements:AppleBlueberrySize of list: 2
Conclusion:
The LinkedList
class in Java provides a wide range of methods to manage, modify, and iterate over its elements. It is particularly useful for scenarios where frequent additions and deletions are required, but it is generally less efficient for random access operations. Understanding these methods allows you to effectively work with linked lists in Java.
Let me know if you'd like any further explanations or examples! ?