Skip to content

Kotlin queue

1. Overview

In this article, we will learn the Kotlin queue interface and its implementations with examples.

A queue is an ordered collection of elements where the addition of elements happens at the rear end whereas the removal of existing elements occurs at the front end. 

  • Enqueue – Insertion of a new element to the queue
  • Dequeue – Removal of the existing elements from the queue
Kotlin queue
Kotlin queue

2. Kotlin queue

Kotlin queue is an interface so you need to use a concrete implementation to use the queue. Below are the Queue implementations available in Kotlin.

  1. LinkedList
  2. PriorityQueue
Kotlin Queue Hierary

As shown in the above diagram, the LinkedList is the implementation of Deque and Queue. A deque, a double-ended queue in which you can add or remove elements from both the ends (rear and front).

However, you can make the Linked List behave as Queue which we will discuss in the next section.

The LinkedList are much standard queue implementation and it stores the queue elements in a standard linked list data structure. Thus, makes the insertion and removal faster.

The PriorityQueue, a subclass of AbstractQueue stores the elements internally according to their natural order based on their Comparable implementation or according to a Comparator passed to the PriorityQueue.

3. Linked List Queue

You can create an Queue object from the LinkedList implementation. For example, the below studentQueue is a Queue object that supports adding elements only at the rear end and removing the element only from the front end.

import java.util.Queue
import java.util.LinkedList

fun main() {
  val studentQueue: Queue<String> = LinkedList<String>(mutableListOf("Agni", "Bob", "Chris", "David", "Emily"))

  println(studentQueue) 

  studentQueue.add("Felix")
  println(studentQueue) 

  studentQueue.remove()
  println(studentQueue) 
  
}

You cannot add an element at the starting of the Queue. When you try to call addFirst method on your queue to add the element at the front, it will show the error “Unresolved reference: addFirst“. However, this function will work for Deque.

  
  studentQueue.addFirst("Afra") 
  println(studentQueue)
  

3.1. Linked List Kotlin Queue remove and get element

You can use either remove or poll functions to remove and return the element from the queue.

The primary difference between both these functions is that remove throws a NoSuchElementException if there are no elements available in the Queue to remove. But, the poll function returns null and doesn’t throw an error.

import java.util.Queue
import java.util.LinkedList

fun main() {
  val studentQueue: Queue<String> = LinkedList<String>(mutableListOf("Agni"))

  val removedElement = studentQueue.remove()
  println(removedElement + " : " + studentQueue) 
  

  studentQueue.poll() 
  println(studentQueue) 


  studentQueue.remove()
  println(studentQueue)
  
}

3.2. Linked List Kotlin Queue get element without removing

You can use either element, or peek function to get the element from the queue. Unlike the poll function, these functions don’t remove the element from the queue.

The primary difference between both these functions is that element throws a NoSuchElementException if there are no elements available in the Queue to fetch. But, the peek function returns null and doesn’t throw any error.

import java.util.Queue
import java.util.LinkedList

fun main() {
  val studentQueue: Queue<String> = LinkedList<String>(mutableListOf("Agni"))

  val element = studentQueue.element()
  println(element + " : " + studentQueue) 
  
  studentQueue.remove() 

  val noneAvailable = studentQueue.peek() 
  println(noneAvailable + " : " +studentQueue) 
  

  studentQueue.element() 
}

4. Conclusion

To sum up, we have seen the LinkedList Queue which is an implementation of the Kotlin Queue.

1 thought on “Kotlin queue”

  1. Pingback: Kotlin Deque implementation with examples - TedBlob

Leave a Reply

Your email address will not be published. Required fields are marked *