GroTechMinds

LinkedList and its methods

LinkedList and its methods

Before we start with LinkedList let us look at Collection Hierarchy

Collection Hierarchy

The below diagram shows the Collection hierarchy consisting of classes and interfaces.

important

As per the scope of the blog our discussion will be limited to the LinkedList class

What is LinkedList class ?

LinkedList class is an implemented class of List interface,Collection interface and Iterable interface as well as the implemented class of  Deque interface and Queue Interface which is used to store multiple elements. It comes from “java.utils” package. LinkedList class is an index based data structure and its underline data structure is “Doubly LinkedList” or “Circular LinkedList”. Duplicate elements can be stored in LinkedList class. LinkedList class allows any number of null values to be stored in it. LinkedList class follow the order of insertion but does not follow sorting order. LinkedList class elements can be iterated in forward and backward direction by  using Iterator and ListIterator methods. LinkedList class is neither thread safe nor  synchronized. In LinkedList  we can insert and delete elements in a faster way due to non synchronization. It grows dynamically and it has no threshold. It occupies more space for storing previous address so it is not memory efficient. For inserting and deleting elements the previous and next nodes need to be managed.

Methods of LinkedList class

Below are some of the methods of LinkedList  class:

Method Name Description
add(Object e) This method is used to add a new element of any datatype into the LinkedList class.
addAll(Collection c) This method is used to add all elements of different datatypes into the LinkedList class.
clear() It is used to clear all the elements from the Set interface but it does not clear the LinkedList class.
contains(Object o) This method is used to check the presence of a Collection element in the LinkedList class. It returns ‘true’ value if the element is present in LinkedList class and ‘false’ value if the element is not present in the LinkedList class.
containsAll(Collection c) This method is used to check if the LinkedList class contains all the collection elements. It returns ‘true’ value if all the elements are present in LinkedList class and ‘false’ value if any one of the element is not present in the LinkedList class.
isEmpty() This method checks if the given LinkedList class is empty
iterator() This method is used to iterate sequentially all the Collection elements present in a LinkedList class.
remove(Object o) This method is used to remove a specified collection element from the LinkedList class.
removeAll(Collection c) This method removes all those Collection elements from the current LinkedList class which are present in another LinkedList class.
retainAll(Collection c) This method retains all those elements from the current LinkedList class which are present in another LinkedList class and removes those elements which are present in current LinkedList class but not in another LinkedList class and vice versa.
size() This method is used to find out and display the number of Collection elements present in the LinkedList class.
toArray() This method is used to convert all collection elements present in LinkedList class into an array.
toString() This method is used to convert all collection elements present in LinkedList class into a String.
equals() This method is used to check if two LinkedList classes are equal. It returns ‘true’ value if elements as well as size of elements present in one LinkedList class is equal to the elements and its size present in another LinkedList class. If the elements and the size of elements present in one Vectro class is not equal to the elements and its size present in another LinkedList class then it returns ‘false’ value.
clone() This method is used to create a shallow copy of the elements present in the LinkedList class.
get(int index) It is used to display an element of LinkedList class present at a particular index.
set(int index,Object element) It is used to replace a LinkedList class element present at a particular index position with another element.
listIterator() This method is used to iterate sequentially in forward as well as backward direction all the Collection elements present in the LinkedList class.
indexOf(Object o) It is used to display the index of first occurrence of LinkedList class element.It gives-1 if the specified element is not present in LinkedList class.
peek() This method is used to fetch or point out the topmost element of the LinkedList which is displayed in output console but does not remove that element from the LinkedList class.
pop() It is used to remove the first element from LinkedList Class.
push(Object item) It is used to add an element at the beginning of the LinkedList.
poll() This method is used to remove the topmost element of the LinkedList which is displayed in output console.
pollFirst() It is used to remove the first element of LinkedList class.
pollLast() It is used to remove the last element of LinkedList class.
getFirst() It is used to display the first element of LinkedList class.
getLast() It is used to display the last element of LinkedList class.
descendingIterator() This method is used to iterate all the Collection elements present in LinkedList in descending order.
peekFirst() It is used to display the first element of LinkedList class.
peekLast() It is used to display the last element of LinkedList class.
offerFirst(Object e) It is used to add an element at the beginning of LinkedList.
offerLast(Object e) It is used to add an element at the end of LinkedList.
offer(Object e) This method is used to add new elements into the LinkedList class.
element() This method is used to fetch or point out the topmost element of the LinkedList which is displayed in output console but does not remove that element from the LinkedList class.
removeFirst() It is used to remove the first element of LinkedList class.
removeLast() It is used to remove the last element of LinkedList class.
addFirst(Object e) It is used to add an element at the beginning of LinkedList.
addLast(Object e) It is used to add an element at the end of LinkedList.

1. Let us check the usage of add(Object e) method in LinkedList class 

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println(a1);
	}
}






Output: [Hi, Hey, 16, 16, 20, 23, 34]


				
			

Screenshot:

l1

2. Let us check the usage of addAll(Collection c) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		LinkedList a2= new LinkedList();
		a2.addAll(a1);
		System.out.println(a2);
	}
}






Output: [Hi, Hey, 16, 16, 20, 23, 34]


				
			

Screenshot:

l2

3. Let us check the usage of clear() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		a1.clear();
		System.out.println(a1);
	}
}






Output: []

				
			

Screenshot:

l3

4. Let us check the usage of contains(Object o) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println("The given LinkedList class contains: "+a1.contains(16));
		System.out.println("The given LinkedList class contains: "+a1.contains("Harish"));
	}
}






Output:  The given LinkedList class contains: true
The given LinkedList class contains: false




				
			

Screenshot:

l4

5. Let us check the usage of containsAll(Collection c) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		LinkedList a2= new LinkedList();
		a2.add("Hi");
		a2.add("Hey");
		a2.add(16);
		a2.add(20);
		System.out.println("Does a2 contains all elements of a1:"+a2.containsAll(a1));
		System.out.println("Does a1 contains all elements of a2:"+a1.containsAll(a2));
	}
}






Output:
Does a2 contains all elements of a1:false
Does a1 contains all elements of a2:true

				
			

Screenshot:

l5

6. Let us check the usage of isEmpty() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			LinkedList a2= new LinkedList();
			System.out.println("Is a1 LinkedList class empty?: "+a1.isEmpty());
			System.out.println("Is a2 LinkedList class empty?: "+a2.isEmpty());
	}
}






Output:

Is a1 LinkedList class empty?: false
Is a2 LinkedList class empty?: true



				
			

Screenshot:

l6

7. Let us check the usage of iterator() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			Iterator a2= a1.iterator();
			System.out.println("The collection elements values are: ");
			while(a2.hasNext())
			{
				System.out.println(a2.next());
			}
	}
}






Output: 

The collection elements values are:
Hi
Hey
16
16
20
23
34





				
			

Screenshot:

v7

8. Let us check the usage of remove(Object o) method in LinkedList class.

Case 1: For removing an element containing words:

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			a1.remove("Hey");
			a1.remove("Hi");
			System.out.println(a1);
	}
}





Output:  [16, 16, 20, 23, 34]


				
			

 Screenshot: 

v8

 Case 2: For removing an element containing numeric value:

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			a1.remove(2);
		      System.out.println("The elements present in LinkedList class after removing 16 from 2nd index:"+a1);
		 a1.remove(4);
			System.out.println("The 4th index is chosen from the list of elements present after removing 16");
			System.out.println("The elements present in LinkedList class after removing 23 from 4th index:"+a1);


	}
}





Output:  The elements present in LinkedList class after removing 16 from 2nd index:[Hi, Hey, 16, 20, 23, 34]
The 4th index is chosen from the list of elements present after removing 16
The elements present in LinkedList class after removing 23 from 4th index:[Hi, Hey, 16, 20, 34]









				
			

Screenshot:

v88

9. Let us check the usage of removeAll(Collection c) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			LinkedList a2= new LinkedList();
			a2.add("Hi");
			a2.add("Hey");
			a2.add(16);
			a2.add(20);
			a1.removeAll(a2);
			System.out.println("Elements present in a1 after removing a2 elements from a1 are:"+a1);
	}
}






Output:
Elements present in a1 after removing a2 elements from a1 are:[23, 34]

				
			

Screenshot:

v9

10. Let us check the usage of  retainAll(Collection c) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			LinkedList a2= new LinkedList();
			a2.add("Hi");
			a2.add("Hey");
			a2.add(16);
			a2.add(20);
			a1.retainAll(a2);
			System.out.println("Elements present in a1 after retaining a2 elements are:"+a1);
	}
}






Output: Elements present in a1 after retaining a2 elements are:[Hi, Hey, 16, 16, 20]



				
			

Screenshot:

v10

11. Let us check the usage of size() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
System.out.println("The size of the collection elements present in the LinkedList class is: "+a1.size());
	}
}






Output: The size of the collection elements present in the LinkedList class is: 7

				
			

Screenshot:

v11

12. Let us check the usage of toArray() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			Object a2[]= a1.toArray();
			System.out.println("The list of array elements are:");
			for(int i=0;i<=a2.length-1;i++)
			{
				System.out.println(a2[i]);
			}
	}
}






Output: The list of array elements are:
Hi
Hey
16
16
20
23
34



				
			

Screenshot:

v12

13. Let us check the usage of toString() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			System.out.println(a1.toString());
	}
}








Output: [Hi, Hey, 16, 20, 23, 34]

				
			

Screenshot:

v13

14.  Let us check the usage of equals() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			LinkedList a2= new LinkedList();
			a2.add("Hi");
			a2.add("Hey");
			a2.add(16);
			a2.add(16);
			a2.add(20);
			a2.add(23);
			a2.add(34);
			LinkedList a3= new LinkedList();
			a3.add("Hi");
			a3.add("Hey");
			a3.add(16);
			a3.add(20);	
			System.out.println("Is LinkedList class a1 equal to a2 ?:"+ a1.equals(a2));
			System.out.println("Is LinkedList class a2 equal to a3 ?:"+ a2.equals(a3));
	}
}





Output: Is LinkedList class a1 equal to a2 ?:true
Is LinkedList class a2 equal to a3 ?:false



				
			

Screenshot:

v14

15. Let us check the usage of clone() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			System.out.println("List of elements in LinkedList class: "+a1);
			LinkedList clonea1= new LinkedList();
			clonea1=(LinkedList)a1.clone();
			System.out.println("List of elements after using clone method:"+ clonea1);
	}
}






Output:  List of elements in LinkedList class: [Hi, Hey, 16, 16, 20, 23, 34]
List of elements after using clone method:[Hi, Hey, 16, 16, 20, 23, 34]



				
			

Screenshot:

v15

16. Let us check the usage of get(int index) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
		System.out.println("List of elements in LinkedList class are: "+a1);
		System.out.println("The element present in index position 2 is: "+a1.get(2));
	}
}














Output: List of elements in LinkedList class are: [Hi, Hey, 16, 16, 20, 23, 34]
The element present in index position 2 is: 16

				
			

Screenshot:

v16

17. Let us check the usage of set(int index,Object element) method in LinkedList class.

  Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			System.out.println("List of elements in LinkedList class are: "+a1);
			System.out.println("The element to be replaced is: "+a1.set(2,56));
	 System.out.println("New List of elements after replacement are :" +a1);
	}
}














Output: List of elements in LinkedList class are: [Hi, Hey, 16, 16, 20, 23, 34]
The element to be replaced is: 16
New List of elements after replacement are :[Hi, Hey, 56, 16, 20, 23, 34]


				
			

Screenshot:

v17

18. Let us check the usage of listIterator() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.ListIterator;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			ListIterator a2= a1.listIterator();
			 while(a2.hasNext()==true)
			 {
			 	 System.out.println("Next method "+a2.next());
			 }
			 while(a2.hasPrevious()==true)
			 {
			 	 System.out.println("Previous Method "+a2.previous());
			 }
	}
}












Output: Next method Hi
Next method Hey
Next method 16
Next method 16
Next method 20
Next method 23
Next method 34
Previous Method 34
Previous Method 23
Previous Method 20
Previous Method 16
Previous Method 16
Previous Method Hey
Previous Method Hi

				
			

Screenshot:

v18

19. Let us check the usage of indexOf(Object o) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			System.out.println("Index of 16 is :"+a1.indexOf(16));
			System.out.println("Index of 45 is :"+a1.indexOf(45));
	}
}














Output: Index of 16 is :2
Index of 45 is :-1





				
			

Screenshot:

v19

20. Let us check the usage of peek() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			System.out.println("Initial List of LinkedList elements: "+a1);	
		System.out.println("Element on which peek method is applied: "+a1.peek());
		System.out.println("New List of LinkedList elements after using peek method: "+a1);
	}
}




Output: 

Initial List of LinkedList elements: [Hi, Hey, 16, 16, 20, 23, 34]
Element on which peek method is applied: Hi
New List of LinkedList elements after using peek method: [Hi, Hey, 16, 16, 20, 23, 34]

				
			

Screenshot:

v20

21.  Let us check the usage of pop() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			System.out.println("List of elements present in LinkedList Class:"+a1);
			System.out.println("Element which is removed "+ a1.pop());
			System.out.println("List of elements present after removal:"+a1);
	}
}










Output: List of elements present in LinkedList Class:[Hi, Hey, 16, 16, 20, 23, 34]
Element which is removed Hi
List of elements present after removal:[Hey, 16, 16, 20, 23, 34]












Output: List of elements present in LinkedList Class:[Hi, Hey, 16, 16, 20, 23, 34]
Element which is removed Hi
List of elements present after removal:[Hi, Hey, 16, 16, 20, 23, 34]




				
			

Screenshot:

v21

22. Let us check the usage of push(Object item) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		 LinkedList a1= new LinkedList();
			a1.add("Hi");
			a1.add("Hey");
			a1.add(16);
			a1.add(16);
			a1.add(20);
			a1.add(23);
			a1.add(34);	
			a1.push(343);
			System.out.println(a1);


	}
}








Output: [343, Hi, Hey, 16, 16, 20, 23, 34]


				
			

Screenshot:

v22

23. Let us check the usage of poll() method in LinkedList class.

Code Snippet:

				
					 package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println("Initial LinkedList elements: "+a1);	
		System.out.println("Removed Element is: "+a1.poll());
		System.out.println("New LinkedList elements after using poll method: "+a1);
	}
}


   Output:  Initial LinkedList elements: [Hi, Hey, 16, 16, 20, 23, 34]
Removed Element is: Hi
New LinkedList elements after using poll method: [Hey, 16, 16, 20, 23, 34]


				
			

Screenshot:

v23

24. Let us check  the usage of pollFirst() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println("Element which is removed: "+a1.pollFirst());
		System.out.println("Remaining elements of LinkedList: "+a1);


	}
}

Output: 
Element which is removed: Hi
Remaining elements of LinkedList: [Hey, 16, 16, 20, 23, 34]

				
			

Screenshot:

v24

25. Let us check the usage of pollLast() method  in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println("Element which is removed: "+a1.pollLast());
		System.out.println("Remaining elements of LinkedList: "+a1);
	}
}



Output: Element which is removed: 34
Remaining elements of LinkedList: [Hi, Hey, 16, 16, 20, 23]


				
			

Screenshot:

v25

26. Let us check the usage of getFirst() method  in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println(a1.getFirst());
	}
}



Output: Hi

				
			

Screenshot:

v26

27. Let us check the usage of  getLast() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println(a1.getLast());
	}
}


Output: 34


				
			

Screenshot:

v27

28. Let us check the usage of descendingIterator() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		Iterator a2= a1.descendingIterator();
		System.out.println("The collection elements values in descending order are: ");
		while(a2.hasNext())
		{
			System.out.println(a2.next());
		}
	}
}


Output:
The collection elements values in descending order are:
34
23
20
16
16
Hey
Hi

				
			

Screenshot:

v28

29. Let us check the usage of  peekFirst() method in LinkedList class.

Code Snippet: 

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println(a1.peekFirst());
	}
}



Output: Hi



				
			

Screenshot:

v29

30. Let us check the usage of peekLast() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println(a1.peekLast());
	}
}



Output: 34

				
			

Screenshot:

v30

31. Let us check the usage of offerFirst(Object e) method  in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.offer("Hi");
		a1.offer("Hey");
		a1.offer(16);
		a1.offer(16);
		a1.offer(20);
		a1.offer(23);
		a1.offer(34);	
		a1.offerFirst(32);
		System.out.println(a1);
	}
}



Output: [32, Hi, Hey, 16, 16, 20, 23, 34]

				
			

Screenshot:

v31

32. Let us check the usage of  offerLast(Object e) method  in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.offer("Hi");
		a1.offer("Hey");
		a1.offer(16);
		a1.offer(16);
		a1.offer(20);
		a1.offer(23);
		a1.offer(34);	
		a1.offerLast(32);
		System.out.println(a1);
	}
}





Output: [Hi, Hey, 16, 16, 20, 23, 34, 32]


				
			

Screenshot:

v32

33. Let us check the usage of (Object e) method works in LinkedList class.

 Code Snippet:

				
					  package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.offer("Hi");
		a1.offer("Hey");
		a1.offer(16);
		a1.offer(16);
		a1.offer(20);
		a1.offer(23);
		a1.offer(34);	
		System.out.println(a1);
	}
}




Output:  [Hi, Hey, 16, 16, 20, 23, 34]

				
			

Screenshot:

v33

34. Let us check the usage of element() method in LinkedList class.

Code Snippet: 

				
					 package linkedList;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34); 
		System.out.println("Initial List of LinkedList elements: "+a1);	
		System.out.println("Element on which element method is applied: "+a1.element());
		System.out.println("New List of LinkedList elements after using element method: "+a1);
	}
}

Output: Initial List of LinkedList elements: [Hi, Hey, 16, 16, 20, 23, 34]
Element on which element method is applied: Hi
New List of LinkedList elements after using element method: [Hi, Hey, 16, 16, 20, 23, 34]

				
			

Screenshot:

v34

35. Let us check the usage of removeFirst() method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println("Element which is removed: "+a1.removeFirst());
		System.out.println("Remaining elements of LinkedList: "+a1);
	}
}


Output: Element which is removed: Hi
Remaining elements of LinkedList: [Hey, 16, 16, 20, 23, 34]


				
			

Screenshot:

v35

36. Let us check the usage of removeLast() method in LinkedList class.

Code Snippet:

				
					 package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		System.out.println("Element which is removed: "+a1.removeLast());
		System.out.println("Remaining elements of LinkedList: "+a1);
	}
}


Output: Element which is removed: Hi
Remaining elements of LinkedList: [Hi, Hey, 16, 16, 20, 23]



				
			

Screenshot:

v36

37. Let us check the usage of  addFirst(Object e) method in LinkedList class.

Code Snippet:

				
					package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		a1.addFirst(345);
		System.out.println(a1);
	}
}


Output:  [345, Hi, Hey, Hello, Hji, 23, 34]
				
			

Screenshot:

v37

38. Let us check the usage of  addLast(Object e) method  in LinkedList class.

Code Snippet:

				
					 package linkedListClass;
import java.util.LinkedList;
public class LinkedListClass {
	public static void main(String[] args) {
		LinkedList a1= new LinkedList();
		a1.add("Hi");
		a1.add("Hey");
		a1.add(16);
		a1.add(16);
		a1.add(20);
		a1.add(23);
		a1.add(34);	
		a1.addLast(345);
		System.out.println(a1);
	}
}



Output:  [Hi, Hey, 16, 16, 20, 23, 34, 345]


				
			

Screenshot:

v38

Difference between ArrayList and LinkedList

SL NO: ArrayList LinkedList
1 It behaves as List only. It behaves as List as well as Deque.
2 The underlined data structure is growable and resizeable array. The underlined data structure is “Doubly LinkedList” or “Circular LinkedList”
3 It is memory efficient and stores only the object in the list. It is not memory efficient and it stores object as well as pointers to next and previous nodes.
4 Best for retrieval operations. Best for insertion and deletion operations.

Difference between LinkedList and Stack

SL NO: LinkedList Stack
1 LinkedList class is an implemented class of List interface,Collection interface and Iterable interface as well as the implemented class of Deque interface and Queue Interface Stack class is an implemented class of List interface,Collection interface and Iterable interface as well as sub class of Vector class
2 It is not a Legacy class It is a Legacy class
3 Belongs to Collection framework Belongs to original version of Java(1.0)
4 Grows dynamically Doubles its size
5 It has no threshold Its threshold is when the current capacity is exceeded
6 It is neither thread safe nor synchronized so it is faster It is both thread safe and synchronized so it is slower.
7 pop() method is used to remove the first element from LinkedList Class. pop() method is used to remove the last element from Stack Class.
8 push(Object item) is used to add an element at the beginning of the LinkedList. push(Object item) is used to add an element at the end of the Stack
9 Iterator,ListIterator are used. Enumeration,Iterator and ListIterator are used.

Conclusion

LinkedList class is used for handling all the Collection elements in an effective way. It is necessary to understand the usage of all the methods of LinkedList class for better handling of Collection elements. By thoroughly understanding the LinkedList class and its methods developers and testers can utitlise those concepts in their real life tasks. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process.Automation testers can utlize all the methods of LinkedList class to create better and effective test scripts for automation testing of web applications and websites. 

Also, read my blogs on:

Upskill Yourself
Consult Us