Java-Linked List with example

The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

The important points about Java LinkedList are: 
  • It is an ordered set comprising a variable number of data items.
  • No need to specify; grow and shrink during execution.
  • Element position is assigned during run time.
  • Stored randomly
  • Insertion and deletion of element will be Easier, fast and efficient.
  • Compared to array ,Linked list required more memory
Hierarchy of LinkedList class



Method & Description


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.

boolean addAll(Collection<? extends E> c):It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

boolean addAll(int index, Collection<? extends E> c):It is used to append all the elements in the specified collection, starting at the specified position of the 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.
Iterator<E> descendingIterator():It is used to return an iterator over the elements in a deque in reverse sequential order.
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.
ListIterator<E> listIterator(int index):It is used to return a list-iterator of the elements in proper sequence, starting at the specified position in the list.
boolean offer(E e):It adds the specified element as the last element of a list.
boolean offerFirst(E e):It inserts the specified element at the front of a list.
boolean offerLast(E e):It inserts the specified element at the end of a list.
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).
<T> T[] toArray(T[] a):It returns an array containing all the elements in the proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
int size():It is used to return the number of elements in a list.

Java LinkedList Example
import java.util.*;

class LinkedListExample {

public static void main(String args[]) {
// Creating object of class arraylist
ArrayList<String> oblist = new ArrayList<>();
// Adding elements to the array list
oblist.add("A");
oblist.add("B");
oblist.add("E");
oblist.add("F");
oblist.add("G");
// Creating object of class linked list by passing arraylist as parameter to constructor
LinkedList<String> list = new LinkedList<String>(oblist);
// Adding elements to the linked list
list.addLast("C");
list.addFirst("D");
list.add("H");
System.out.println("Linked list : " + list);
// Removing elements from the linked list
list.remove("B");
list.remove(3);
list.removeFirst();
list.removeLast();
System.out.println("Linked list after deletion: " + list);
// Finding elements in the linked list
boolean status = list.contains("F");
if (status)
System.out.println("List contains the element 'F' ");
else
System.out.println("List doesn't contain the element 'F'");
// Number of elements in the linked list
int size = list.size();
System.out.println("Size of linked list = " + size);
// Get and set elements from linked list
Object element = list.get(2);
System.out.println("Element returned by get() : " + element);
list.set(2, "Y");
System.out.println("Linked list after change : " + list);
}
}

Output:

Linked list : [D, A, B, E, F, G, C, H]
Linked list after deletion: [A, E, G, C]
List doesn't contain the element 'F'
Size of linked list = 4
Element returned by get() : G
Linked list after change : [A, E, Y, C]


This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - DES Encryption and Decryption example

Java - Blowfish Encryption and decryption Example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Top 5 Java ORM tools - 2024

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String