LinkedList Java
The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class is not synchronized in the linked list, and there is no need for shifting in the linked list, so manipulation is faster. It is used as the list, stack, and queue. In the linked list, doubly linked list means we can insert or delete from both sides of the list. It is the collection of nodes and every node consist of three parts.
- One part of the data.
- The second part to reference the next node.
- And third part to reference the previous node.
Declaration of Linked List
The declaration of the linked list is the same as the array list declaration. We can use a generic collection to declare the linked list.
LinkedList<String> li=new LinkedList<String> ();
li- it holds the reference of objects.
The constructor of Linked List
There are two types of Constructor in the linked list:
- LinkedList (): This is used to make an empty linked list.
- LinkedList (Collection c): This is used to create an ordered list and contains all elements of specified collections.
Methods of Linked List
- addFirst (Element e): This method is used to insert the element in the starting of the list.
- addLast (Element e): This method is used to insert the element in the last of the list.
- addAll (Collection c): It is used to insert all elements at the end of the list.
- addAll (int index, Collection c): It is used to insert all elements from the specific index in the list.
- add (Element e): It is used to insert the element from the end of the list.
- add (Int index, element e): This method is used to insert the element from the specific position in the list.
- clone (): This method is used to return the same copy of the list.
- remove (): This method is used to remove the first element of the list.
- remove (int index): It is used to eliminate the element at a specific position from the list.
- remove (object o): It is used to delete the specific element from the list, and it only eliminates the first occurrence of the element.
- removeFirst (): This method is used to remove and give back the first element of the list.
- removeFirstOccurrence (object o): It is used to eliminate the first occurrence of an element from the list.
- removeLast (): This method is used to remove and give back the last element of the list.
- removeLastOccurrence (): This method is used to remove the last occurrence of an element from the list.
- set (int index, element e): This method is used to set the element at the specific position and replaced that element.
- get (int index): This method is used to give back the element at the specific index.
- getFirst (): This method is used to give back the first element of the list.
- getLast (): This method is used to give back the last element of the list.
- clear (): This method is used to remove the elements from the list.
- contains (): It is used to check the element is in the list or not. It returns true or false.
- IndexOf (object o): It is used to give back the index of the element.
- lastIndexOf (object o): It is used to give back the last index of the element.
- Size (): This method is used to give the size of the list.
Example of linked list using iterator:
import java.util.*;
public class LinkedList1 {
public static void main (String args []) {
LinkedList<String> al=new LinkedList<String> ();
al.add ("java");
al.add ("python");
al.add ("angular");
al.add ("mongo db");
al.addFirst ("R");
Iterator<String> itr=ar.iterator ();
while (itr.hasNext ()){
System.out.println (itr.next ());
} } }
Output
R
java
python
angular
mongo db
Reverse a list of elements
import java.util.*;
public class ReverseExample {
public static void main (String args []) {
LinkedList<String> al=new LinkedList<String> ();
al.add ("Mongodb");
al.add ("Nodejs");
al.add ("React");
Iterator i=ll.descendingIterator ();
while (i.hasNext ()) {
System.out.println (i.next ());
} } }
Output
React
Nodejs
Mongodb
ListIterator Example
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main (String[] args) {
LinkedList<String> al = new LinkedList<String> ();
al.add ("My");
al.add ("Name");
al.add ("is ");
al.add ("Khan");
ListIterator listIt =al.listIterator ();
System.out.println ("Forward iteration :");
while (listIt.hasNext ()) {
System.out.println (listIt.next ());
}
System.out.println ("\nBackward iteration :");
while (listIt.hasPrevious ()){
System.out.println (listIt.previous ());
} } }
Output
Forward iteration:
My
Name
is
Khan
Backward iteration:
Khan
is
Name
My
Example of using different adds methods in Linked List:
import java.util.*;
public class AddMethod {
public static void main (String args []) {
LinkedList<String> al=new LinkedList<String> ();
System.out.println ("Initial list of elements: "+al);
al.add ("Noida");
al.add ("Aligarh");
al.add ("Mathura");
System.out.println ("After Step1: "+al);
al.add (1, "Gujarat");
System.out.println ("After Step2: "+al);
LinkedList<String> al2=new LinkedList<String> ();
al2.add ("Sonipat");
al2.add ("Ajmer");
al.addAll (al2);
System.out.println ("After Step3: "+al);
LinkedList<String> al3=new LinkedList<String> ();
al3.add ("Agra");
al3.add ("Delhi");
al.addAll (1, al3);
System.out.println ("After Step4: "+al);
al.addFirst ("Gurgaon");
System.out.println ("After Step5: "+al);
al.addLast ("Himachal Pradesh");
System.out.println ("After Step6: "+al);
} }
Output
Initial list of elements: []
After Step1: [Noida, Aligarh, Mathura]
After Step2: [Noida, Gujarat, Aligarh, Mathura]
After Step3: [Noida, Gujarat, Aligarh, Mathura, Sonipat, Ajmer]
After Step4: [Noida, Agra, Delhi, Gujarat, Aligarh, Mathura, Sonipat, Ajmer]
After Step5: [Gurgaon, Noida, Agra, Delhi, Gujarat, Aligarh, Mathura, Sonipat, Ajmer]
After Step6: [Gurgaon, Noida, Agra, Delhi, Gujarat, Aligarh, Mathura, Sonipat, Ajmer, Himachal Pradesh]
Examples of using set and get methods in the Linked List:
import java.util.LinkedList;
public class GetandSet {
public static void main (String [] args) {
LinkedList<String> gs = new LinkedList<String> ();
gs.add ("Item1");
gs.add ("Item2");
gs.add ("Item3");
gs.add ("Item4");
gs.add ("Item5");
gs.add ("Item6");
Object setelement = gs.set (1,"test");
System.out.println (gs);
Object firstElement = gs.getFirst ();
System.out.println ("First Element is: "+firstElement);
Object lastElement =gs.getLast ();
System.out.println ("Last Element is: "+lastElement);
} }
And Output is:
[Item1, test, Item3, Item4, Item5, Item6]
First Element is: Item1
Last Element is: Item6