Difference between a Static Queue and a Singly Linked List in Java.

Java 8Object Oriented ProgrammingProgramming

In Java List and Queue both are introduced as an ordered list of objects, where the same object may be added more than once. The difference between both comes in the manner of adding elements. In the queue, all the elements get inserted at the rear and removed from the front while we can add an element anywhere in the list.

Sr. No.KeyStatic QueueSingly Linked List
1Data initialization.Static Queue works in first out(FIFO) fashion as all the elements get inserted at the REAR and removed from the FRONT of the queue.In the case of Singly Linked List, one can add elements anywhere in the list and also can get any element based on its index in the list.
2Internal implementation.Internally queue has implemented array which makes it faster for searching and addition of elements.On the other hand, list maintains node and pointers for storing data and address of the next node
3SizeStatis queue is fixed in size and could not be altered. The size of queue is defined at the time of its declaration.List does not have fixed size and so no size is required at the time of declaration of list.
4PerformanceThe static queue is faster in case of searching for an element while the deletion of an element is slower because deletion requires shifting of all the remaining elements to the front by one position.The list is slower in searching due to its node-based implementation but deletion is faster as the only the address of pointer of one node is need to update instead of updating in the whole list.
5Data migration.The static queue is always based on First in first out (FIFO) technique.On the other hand List can be FIFO or Last in First out (LIFO).

Example of Static queue and Singly Linked List.

StaticQueueDemo.java

 Live Demo

import java.util.*;
public class StaticQueueDemo {
   public static void main(String args[]) {
      PriorityQueue < String > queue = new PriorityQueue < String > ();
      queue.add("Jack");
      queue.add("Smith");
      queue.add("John");
      System.out.println("head:" + queue.element());
      System.out.println("head:" + queue.peek());
      System.out.println("iterating the queue elements:");
      Iterator itr = queue.iterator();
      while (itr.hasNext()) {
         System.out.println(itr.next());
      }
      queue.remove();
      queue.poll();
      System.out.println("after removing two elements:");
      Iterator < String > itr2 = queue.iterator();
      while (itr2.hasNext()) {
         System.out.println(itr2.next());
      }
   }
}

Output

head:Jack
head:Jack
iterating the queue elements:
Jack
Smith
John
after removing two elements:
Smith

Example

LinkedListDemo.java

 Live Demo

import java.util.*;
public class LinkedListDemo {
   public static void main(String[] args) {
      // Creating a LinkedList
      LinkedList < String > friends = new LinkedList <> ();
      // Adding new elements to the end of the LinkedList using add() method.
      friends.add("John");
      friends.add("Jack");
      friends.add("Smith");
      friends.add("Chris");
      System.out.println("Initial LinkedList : " + friends);
      // Adding an element at the specified position in the LinkedList
      friends.add(3, "Lisa");
      System.out.println("After add(3, \"Lisa\") : " + friends);
   }
}

Output

Initial LinkedList : [John, Jack, Smith, Chris]
After add(3,"Lisa") : [John, Jack, Smith, Chris, Lisa]
raja
Published on 16-Sep-2019 11:10:29
Advertisements