LinkedList in java

LinkedList in java

In this tutorial, we are going to discuss LinkedList in java. Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations, and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node.

In the previous chapter, we are discussed about the ArrayList class. The LinkedList class is almost identical to the ArrayList.

  • The underlying data structure is Double LinkedList
  • Insertion order is preserved.
  • Duplicate objects are allowed.
  • Heterogeneous objects are allowed.
  • null insertion is possible.
  • Java LinkedList class is non synchronized.
  • Implements Serializable and Cloneable interfaces but not RandomAccess.
  • LinkedList is the best choice if our frequent operation is an insertion (or) deletion in the middle.
  • If our frequent operation is retrieval operation, then LinkedList is the worst choice.
LinkedList in java

Usually we can use LinkedList to implement Stacks and Queues.

To provide support for this requirement LinkedList class defines the following 6 specific methods.

  • void addFirst(Object o);
  • void addLast(Object o);
  • Object getFirst();
  • Object getLast();
  • Object removeFirst();
  • Object removeLast();

We can apply these methods only on LinkedList object.

Constructors

To create a LinkedList, we need to create an object of the LinkedList class. The LinkedList class consists of various constructors that allow the possible creation of the list. The following are the constructors available in this class.

1. LinkedList()

This constructor is used to create an empty linked list. If we wish to create an empty LinkedList with the name l, then it can be created as:

LinkedList l=new LinkedList();

So above syntax will create an empty LinkedList object.

2. LinkedList(Collection C)

This constructor is used to create an ordered list that contains all the elements of a specified collection, as returned by the collection’s iterator. If we wish to create a LinkedList with the name l, then it can be created as

LinkedList l=new LinkedList(Collection c);

So above syntax will create an equivalent LinkedList object for the given collection.

package com.ashok.collections;

import java.util.LinkedList;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyLinkedList {
   public static void main(String[] args) {
      LinkedList list = new LinkedList();
      list.add("Ashok");
      list.add(30);
      list.add(null);
      list.add("Ashok");
      System.out.println(list);// [Ashok, 10, null, Ashok]
      list.set(0, "Software");
      System.out.println(list);// [Software, 10, null, Ashok]
      list.set(0, "Vinod");
      System.out.println(list);// [Vinod, 10, null, Ashok]
      list.removeLast();
      System.out.println(list);// [Vinod, 10, null]
      list.addFirst("Dillesh");
      System.out.println(list);// [Dillesh, Vinod, 30, null]
   }
}
Inter conversion between collection objects
package com.ashok.collections.linkedlist;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyCollectionsConversion {
   public static void main(String a[]) {
      ArrayList arrayList = new ArrayList();
      arrayList.add(10);
      arrayList.add(20);
      arrayList.add(30);
      System.out.println("l1--->" + arrayList);
   
      LinkedList linkedList = new LinkedList(arrayList);
      linkedList.add(1, 5);
      linkedList.add(3, 5);
      linkedList.add(5, 15);
      System.out.println("l2--->" + linkedList);
   
      ArrayList arrayList1 = new ArrayList(linkedList);
      System.out.println("l3--->" + arrayList1);
   }
}
Output
l1--->[10, 20, 30]
l2--->[10, 5, 20, 5, 30, 15]
l3--->[10, 5, 20, 5, 30, 15]
How Does LinkedList work Internally?

Since a LinkedList acts as a dynamic array and we do not have to specify the size while creating it, the size of the list automatically increases when we dynamically add and remove items. And also, the elements are not stored continuously. Therefore, there is no need to increase the size.

Internally, the LinkedList is implemented using the doubly linked list data structure. The main difference between a regular linked list and a doubly LinkedList is that a doubly linked list contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in the singly linked list.

When To Use

Use an ArrayList for storing and accessing data and LinkedList to manipulate data.

LinkedList in java

Scroll to top