LinkedList in Java

LinkedList  is an implementation of the interface java.util.List seen in  ArrayList  and  Vector. LinkedList represents a linked list in java.

The LinkedList class supports two constructors:

LinkedList()
This constructor creates an empty LinkedList.

LinkedList(Collection c)
This constructor creates an initialized LinkedList with a collection of data.

Methods of the LindekList class

1)  void add(Object o): Adds an item to the list.

list. Ladd("hello"); 
2)  void add(int index, Object o) : adds an element to a defined position.


list.add(3, "position3"); 
3)  void addAll(Collection c): Adds items from another data collection such as ArrayList. It throws an exception. NullPointerException  if the collection is null.

LinkedList linkedlist = new LinkedList(); 
ArrayList arraylist= new ArrayList();
arraylist.add("123");
arraylist.add("456");
linkedlist.addAll(arraylist);
4)  void addAll(int index, Collection c): Adds items from another data collection such as ArrayList starting from a given position. It throws an exception. NullPointerException  if the collection is zero and  IndexOutOfBoundsException  If you have exceeded the capacity of the .

linkedlist.addAll(3, arraylist) capacity. 
5)  void clear(): clears the contents of the list.

list.clear(); 
6)  Object clone(): returns a copy of the list.

System.out.println("linkedList: "+list); 
Object str= list.clone();
System.out.println("str: "+str);
Output:

linkedList: [object1, object2, object3]
str: [object1, object2, object3]
7)  boolean contains(Object o): Checks if the object is present in the list. If the element exists, it returns true otherwise false.

boolean var = list.contains("String"); 
8)  Object get(int index): returns the element to the given index.
Object elt = llist.getLast(); 
9)  int indexOf(Object o): returns the index of a given object.

int pos = llist.indexOf("o2"); 
10)  int lastIndexOf(Object o): returns the index of the last occurrence of a given object.

int lastpos = llist.lastIndexOf("o6"); 
11)  Object remove(int indice): deletes an object with the given index.

llist.remove(4); 
12)  Object remove(Object o): Removes a specific object from the list.

llist.remove("o6"); 
13)  Object removeFirstOccurrence(Object o): deletes the first occurrence encountered.

llist.removeFirstOccurrence("hello"); 
14)  Object removeLastOccurrence(Object o): Deletes the last occurrence encountered.

llist.removeLastOccurrence("hello"); 
15)  Object set(int indice, Object o): change the value of an element to a specific index.

llist.set(llist.sise()-1, "good evening"); 
Put "good evening" in the last position of the list, we put "-1" so as not to exceed the size of the list.

16)  int size(); returns the current size or total number of objects in the list.

llist.size(); 

LinkedList-specific methods

1)  void addFirst(Object o): inserts an element in the first position.

list.addFirst("string"); 
2)  void addLast(Object o): inserts an element in the last position.

list.addLast("string"); 
3)  Object getFirst(): returns the element to the first position.

Object elt = llist.getFirst(); 
4)  Object getLast(): returns the element to the last position.

list.addFirst("string"); 
5)  void removeFirst(): removes the element from the first position.

list.removeFirst(); 
6)  void removeLast(): removes the element from the last position.

list.removeLast(); 

LinkedList example

This example shows some of the most popular methods supported by LinkedList:

LinkedList package; 
import java.util.*;

public class LinkedListDemo {

public static void main(String args[]) {
// linked list declaration
LinkedList ll = new LinkedList();
// fill in the elements in linked list
ll.add("C");
ll.add("D");
ll.add("T");
ll.add("V");
ll.addFirst("A");
ll.addLast("Z");
ll.add(1, "B");

System.out.println("Original content: " + ll);

// remove elements from the linked list
ll.remove("F");
ll.remove(2);
ll.removeFirst();
System.out.println("After deletion: "
+ ll);

// change the object's value to 3
String first = ll.getFirst();
int index = ll.indexOf(first);
ll.set(index, first + "New");
System.out.println("After modification: " + ll);
}
}
Output:

Original content: [A, B, C, D, T, V, Z]
After deletion: [B, D, T, V, Z]
After modification: [B New, D, T, V, Z]
References:
Javadoc: LinkedList
Tutorialspoint: LinkedList class
upmf-grenoble: LinkedList