Java LinkedList class - IT magazine

IT magazine

Knowledge that matters

Java LinkedList class

Share This

Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.

The important points about Java LinkedList are:
  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • Java LinkedList class is non synchronized.
  • In Java LinkedList class, manipulation is fast because no shifting needs to occur.
  • Java LinkedList class can be used as a list, stack or queue.

Hierarchy of LinkedList class

As shown in the above diagram, Java LinkedList class extends AbstractSequentialList class and implements List and Deque interfaces.


The Java LinkedList class provides a doubly linked list implementation.

Each element in a linked list is known as a node. It consists of 3 fields:

Prev - Stores an address of the previous element in the list. It is null for the first element.
Next - Stores an address of the next element in the list. It is null for the last element.
Data - Stores the actual data.
Elements in linked lists are not stored in sequence. Instead, they are scattered and connected through links (Prev and Next).

Here we have 3 elements in a linked list.

Dog - it is the first element that holds null as previous address and address of Cat as next address
Cat - it is the second element that holds an address of Dog as previous address and address of Cow as next address
Cow - it is the last element that holds the address of Cat as the previous address and null as the next element

Creating a LinkedList

Here is how we can create linked lists in Java:

LinkedList<Type> linkedList = new LinkedList<>();
Here, Type indicates the type of a linked list. For example,

// create Integer type linked list
LinkedList<Integer> linkedList = new LinkedList<>();

// create String type linked list
LinkedList<String> linkedList = new LinkedList<>();


Various Methods of LinkList class:


boolean add(E e)
It is used to append the specified element to the end of a list.
void add(int index, E element)
It is used to insert the specified element at the specified position index in a list.
void addFirst(E e)
It is used to insert the given element at the beginning of a list.
void addLast(E e)
It is used to append the given element to the end of a list.
void clear()
It is used to remove all the elements from a list.
Object clone()
It is used to return a shallow copy of an ArrayList.
boolean contains(Object o)
It is used to return true if a list contains a specified element.
E element()
It is used to retrieve the first element of a list.
E get(int index)
It is used to return the element at the specified position in a list.
E getFirst()
It is used to return the first element in a list.
E getLast()
It is used to return the last element in a list.
int indexOf(Object o)
It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element.
int lastIndexOf(Object o)
It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element.
E peek()
It retrieves the first element of a list
E peekFirst()
It retrieves the first element of a list or returns null if a list is empty.
E peekLast()
It retrieves the last element of a list or returns null if a list is empty.
E poll()
It retrieves and removes the first element of a list.
E pollFirst()
It retrieves and removes the first element of a list, or returns null if a list is empty.
E pollLast()
It retrieves and removes the last element of a list, or returns null if a list is empty.
E pop()
It pops an element from the stack represented by a list.
void push(E e)
It pushes an element onto the stack represented by a list.
E remove()
It is used to retrieve and removes the first element of a list.
E remove(int index)
It is used to remove the element at the specified position in a list.
boolean remove(Object o)
It is used to remove the first occurrence of the specified element in a list.
E removeFirst()
It removes and returns the first element from a list.
boolean removeFirstOccurrence(Object o)
It is used to remove the first occurrence of the specified element in a list (when traversing the list from head to tail).
E removeLast()
It removes and returns the last element from a list.
boolean removeLastOccurrence(Object o)
It removes the last occurrence of the specified element in a list (when traversing the list from head to tail).
E set(int index, E element)
It replaces the element at the specified position in a list with the specified element.
Object[] toArray()
It is used to return an array containing all the elements in a list in proper sequence (from first to the last element).
int size()
It is used to return the number of elements in a list.


Example:
import java.util.*;
class LinkDemo 
{
    public static void main(String[] args)
     {
        LinkedList<String> animals = new LinkedList<>();

        // Add elements to LinkedList
        animals.add("Dog");
        animals.add("Cat");
        animals.add("Horse");
        System.out.println("LinkedList: " + animals);
    }
}

No comments:

Post a Comment