LinkedList in Java

In previous tutorials, We have learned about the ArrayList class of the Java Collection. In this tutorial, we will learn about the LinkedList class of the Java Collection.

LinkedList is a class which implements the List interface of collection framework.

  • Java LinkedList class uses doubly linked list to store the elements.
  • Like ArrayList, LinkedList also can contain the duplicate elements and also maintain the insertion order.
  • Like ArrayList, LinkedList is also non-synchronized.

 

We can declare the LinkedList same like ArrayList and some other operation are like insertion and deletion are same like ArrayList.

The only difference in LinkedList and ArrayList is, LinkedList stores elements in a doubly-linked list data structure while ArrayList stores elements in a backing array.

LinkedList offers constant time for adding and removing elements at the end of the list; and linear time for operations at other positions in the list. Therefore, we can consider using a LinkedList if fast adding and removing elements at the end of the list is required.

Let’s see an example of LinkedList –

Output:

Linked List Content — [Anshul, Merilla, Eden, Matt, Abhishek]
LinkedList Content after addition — [First Element, Anshul, Merilla, Eden, Matt, Abhishek, Last Element]
First element: First Element
First element after update — Changed first item
LinkedList after deletion of first and last element — [Anshul, Merilla, Eden, Matt, Abhishek]

 

In the above example, we have seen the basic operations on LinkedList. Let’s understand this, First we have declared the LinkedList like this –

LinkedList<String> linkedlist = new LinkedList<String>();

Then we have performed some basic example like addition, deletion and Add Element at First and Last index in LinkedList. Replace the specific elements in LinkedList and finally Print the LinkedList.

We will discuss the important methods of the linked list in the coming tutorials.

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply