Linked list is a data structure consisting of group of nodes which together represent a sequence. Each node has data part and reference to the next node in the sequence.
This structure makes insertion and removal of elements from any position in the sequence an effortless process.The last node is linked to a terminator used to signify the end of the list.
They can be used to implement several other abstract data types, including lists, stacks, queues, associative arrays, and S-expressions. It is very common way of storing arrays of data. The major benefit of linked lists is that we don’t have to specify its size in advance, the more elements you add to the chain, the bigger the chain gets.
LinkedList Methods In JAVA:
Let us discuss all the LinkedList methods one by one with Examples in Java.
1. void add(int index, Object element):
This method adds element of Specific Object type at the specified index of the Linked List as mentioned the method. If in case the index specified is out of range it throws an IndexOutOfBounds Exception. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("LinkedList:" + llist); //Using Add method at specific index llist.add(1,"Element"); System.out.println("Linked List:" + llist); } }
Output:
LinkedList: [Hi, I, Love, java] LinkedList: [Hi, Element, I, Love, java]
2. boolean add(Object o):
It adds an element of Specific Object type at the end of Linked List . It gives True if element is successfully added, and gives false if it is not. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("LinkedList:" + llist); // using add method llist.add("Element"); System.out.println("Linked List:" + llist); } }
Output:
Linked List: [Hi, I, Love, java, Element]
3. boolean addAll(Collection c):
This method adds each element of the given collection at the end of the Linked List, It returns True if collection is added successfully, and returns false if it is not. If the collection passed is null then Null Pointer Exception is thrown. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List:" + llist); Collection collection = new ArrayList(); collection.add("I"); collection.add("Love"); collection.add("Android"); // using method addAll() llist.addAll(collection); System.out.println("Linked List:" + llist); } }
Output:
Linked List:[Hi, I, Love, java, I, Love, Android]
4. boolean addAll(int index, Collection c):
This methods add each element of the Specific collection type at index given in the argument. It returns True if collection is successfully added, and returns false if not. If the collection passed is null then Null Pointer Exception is thrown. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List:" + llist); Collection collection = new ArrayList(); collection.add("I"); collection.add("Love"); collection.add("Android"); // using method addAll() at index 3 llist.addAll(3,collection); System.out.println("Linked List:" + llist); } }
Output:
Linked List:[Hi, I, Love, I, Love, Android, java]
5. void addFirst(Object o):
This method add the specified element at the first position of the list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List:" + llist); // add a new element at first position llist.addFirst("First"); // print the new list System.out.println("LinkedList:" + llist); } }
Output:
Linked List:[Hi, I, Love, java] LinkedList:[First, Hi, I, Love, java]
6. void addLast(Object o):
This method add the specified element at the last position of the list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List:" + llist); // add a new element at first position llist.addLast("Last"); // print the new list System.out.println("LinkedList:" + llist); } }
Output:
Linked List:[Hi, I, Love, java] LinkedList:[Hi, I, Love, java, Last]
7. void clear():
This method removes all the elements of the Linked List. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List:" + llist); // clear the list llist.clear(); System.out.println("Linked List:" + llist); } }
Output:
Linked List:[Hi, I, Love, java] Linked List:[]
8. Object clone():
This method returns the exact same copy of the Linked List object. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist1 = new LinkedList(); llist1.add("Hi"); llist1.add("I"); llist1.add("Love"); llist1.add("java"); System.out.println("Linked List 1:" + llist1); LinkedList llist2 = new LinkedList(); // clone llist1 llist2 = (LinkedList) llist1.clone(); // print list2 System.out.println("Linked List 2:" + llist2); } }
Output:
Linked List1:[Hi, I, Love, java] Linked List 2:[Hi, I, Love, java]
9. boolean contains(Object o):
This method returns true if the calling linked list object has the specific element present as given in the argument list, otherwise it returns false. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // check if the list contains Android System.out.println("List contains 'Android':" + llist.contains("Android")); // check if the list contains java System.out.println("List contains 'java':" + llist.contains("java")); } }
Output:
Linked List :[Hi, I, Love, java] List contains 'Android':false List contains 'java':true
10. Object get(int index):
It returns the element present in the mentioned position in the linked list. If the index mentioned in the argument is more than the size of linked list, then it throws Index Out of Bound Exception. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // print element at index 2 System.out.println("Element at index 3 :" + llist.get(2)); } }
Output:
Linked List :[Hi, I, Love, java] Element at index 3 :Love
11. Object getFirst():
This method returns the first element in this linked list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // print the first element using getFirst() method System.out.println("First Element :" + llist.getFirst()); } }
Output:
Linked List :[Hi, I, Love, java] First Element :Hi
12. Object getLast():
This method returns the last element in this linked list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // print the first element of the list System.out.println("Last Element :" + llist.getLast()); } }
Output:
Linked List :[Hi, I, Love, java] Last Element :java
13. int indexOf(Object o):
This method returns the index of the element given in the linked list starting from first position. Otherwise it will return -1, if that element is not present in the linked list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // get the index for "I" System.out.println("Index for Chocolate:" + llist.indexOf("I")); // get the index for "Android" System.out.println("Index for Coffee:" + llist.indexOf("Android")); } }
Output:
Linked List :[Hi, I, Love, java] Index for Chocolate:1 Index for Coffee:-1
14. int lastIndexOf(Object o):
This method gives the index of the element as mentioned in the linked list starting from last position. Otherwise it will return -1 , if that element is not present in the linked list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); llist.add("Hi"); System.out.println("Linked List :" + llist); // get the last index for "Hi" System.out.println("Last Index for Hi: " + llist.lastIndexOf("Hi")); // get the index for "Android" System.out.println("Last Index for Android: " + llist.lastIndexOf("Android")); } }
Output:
Linked List :[Hi, I, Love, java, Hi] Last Index for Hi: 4 Last Index for Android: -1
15. ListIterator listIterator(int index):
This method returns an object of Iterator class that contains the elements in proper sequence starting from the specified position as mentioned in the argument . Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); llist.add("Hi"); System.out.println("Linked List :" + llist); // set Iterator at specified index Iterator itr = llist.listIterator(3); while (itr.hasNext()) { System.out.println(itr.next()); } } }
Output:
Linked List :[Hi, I, Love, java, Hi] java Hi
16. Object remove(int index):
It deletes the element from the given index from the Linked List. It returns an IndexOutOfBoundsException if index specified is out of range. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // remove the element at index 3 System.out.println("Element to be removed:" + llist.remove(3)); System.out.println("LinkedList:" + llist); } }
Output:
Linked List :[Hi, I, Love, java] Element to be removed:java LinkedList:[Hi, I, Love]
17. boolean remove(Object o):
This method deletes the first occurrence of given element in argument list from the linked list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Hi"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // remove first occurance of "Hi" System.out.println("Hi is in the list:" + llist.remove("Hi")); // print the list System.out.println("LinkedList:" + llist); } }
Output:
Linked List :[Hi, I, Hi, Love, java] Hi is in the list:true LinkedList:[I, Hi, Love, java]
18. Object removeFirst():
This method retrieves and removes the head element from this linked list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // remove the head element System.out.println("First element:" + llist.removeFirst()); System.out.println("Linked List :" + llist); } }
Output:
Linked List :[Hi, I, Love, java] First element:Hi Linked List :[I, Love, java]
19. Object removeLast():
This method retrieves and removes the last element from this linked list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // remove the last element System.out.println("Last element:" + llist.removeLast()); // print the list System.out.println("LinkedList:" + llist); } }
Output:
Linked List :[Hi, I, Love, java] Last element:java LinkedList:[Hi, I, Love]
20. Object set(int index, Object element):
This method replaces the content at index given with the element given in argument list. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List :" + llist); // set "Android" at index 3 System.out.println("Object to be replaced:" + llist.set(3, "Android")); // print the list System.out.println("Linked List:" + llist); } }
Output:
Linked List :[Hi, I, Love, java] Object to be replaced:java LinkedList:[Hi, I, Love, Android]
21. int size():
This method returns the size of the Linked List. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); // print the size of the linked list System.out.println("linked List size:" + llist.size()); } }
Output:
linked List size:4
22. Object[] toArray():
This method converts the linked list to its corresponding array. Example of this method is shown below:
import java.util.*; public class LinkedListMethods { public static void main(String[] args) { LinkedList llist = new LinkedList(); llist.add("Hi"); llist.add("I"); llist.add("Love"); llist.add("java"); System.out.println("Linked List:" + llist); // create an array and copy the list to it Object[] array = llist.toArray(); // print the array for (int i = 0; i < llist.size(); i++) { System.out.println("Array:" + array[i]); } } }
Output:
Linked List:[Hi, I, Love, java] Array:Hi Array:I Array:Love Array:java