Updated March 30, 2023
Introduction of Kotlin Queue
The kotlin queue is one of the data structure concepts. It is applicable for FIFO(First In First Out) operation so whenever we want to add the new elements to the list. It will be added on the backend of the memory list also; if we remove any of the elements, it first removes the front of the list index also to be removed from the list, it follows the enqueue that is to be inserting elements, and dequeue is removed from the elements. The kotlin queue interface added the collection framework associated with the classes and other methods.
Syntax
In kotlin language has many default classes, and its methods for each and every framework collections interface will implement the application. So the queue is nothing, but it can insert the values in the memory list using the first-in-first-out(FIFO) format.
import java.util.Queue
import java.util.LinkedList
fun main()
{
val variablename: Queue<datatype> = LinkedList<datatype>()
variablename.add(values based on the datatype)
-----somecodes depends on the logic---
}
The above code is the basic syntax for implementing the queue interface in the kotlin application. We can add default methods like add(), remove(), etc., based on the queue collection interface and classes.
How does Queue work in Kotlin?
The kotlin queue works as the FIFO concept like a first-in-first-out operation, so the data will be stored at the backend using the add() operation. If suppose when we want to remove the data in the list, we can use the remove() operation, and the data will be removed at the front end. Whenever we want to add or remove the elements in the queue list, it should be validated if the queue is an empty string. So that we can calculate the queue size, the length should be identified so that it will be validated according to the logic it’s used for to perform the operations in the application. Every element will have a separate index for to store and retrieve the datas from the db. In the Queue concept, the elements are also stored with the separate index on the memory location, so we will check the queue contains all the elements as the separate index and find the index using the index value. We can get the element at the front of the queue memory without removing operations, so they used the operations called element() and peek(). These methods will perform the separate operations; it has some differences like if the queue is empty, the element() method will throw the error, but when we use the peek() method, it throws only null value. So these two methods are more identically used it on the queue collection interface.
Examples of Kotlin Queue
Different examples are mentioned below:
Example #1
Code:
class month (lst:MutableList<Any>){
var vars:MutableList<Any> = lst
fun demo():Boolean = vars.isEmpty()
fun demo1():Int = vars.count()
override fun toString() = vars.toString()
fun enqueue(element:Any){
vars.add(element)
}
fun dequeue():Any?{
if (this.demo()){
return null
} else {
return vars.removeAt(0)
}
}
fun peek():Any?{
return vars[0]
}
}
fun main(args: Array<String>) {
val fr = 32.45f
val sc = 12.3f
val res = fr * sc
println("Thank you users for spending the time with our application $res")
var ex = 0
var n1 = 123456789
while (n1 != 0) {
n1 /= 15
++ex
}
println("Your net output result is calculated: $ex")
var x = month(mutableListOf("Welcome To My Domain its the first example that related to the kotlin queue concept",7,9,"Have x nice day users please try again"))
println(x.enqueue(6))
println(x)
println(x.dequeue())
println(x)
println(x.peek())
println(x)
println(x.demo1())
println(x.demo())
}
Output:
In the above example, we used the queue concept and joined a mutable list interface to store and retrieve the data operations.
Example #2
Code:
class SecondExam {
private val sizes = 8
private var arraqueues: IntArray = IntArray(this.sizes)
private var varfirst = 1
private var varlast = -1
private var latestsize = 0
fun enqueue(ex: Int) {
if (exam()) {
println("Welcome Users your queue is fill you want to increase the queueu sizes")
exam()
}
varlast++
if (varlast >= arraqueues.size && latestsize !== arraqueues.size) {
varlast = 0
}
arraqueues[varlast] = ex
latestsize++
println("Thank you users your element is added on the queue size: $ex")
}
fun dequeue() {
if (isQueueEmpty()) {
println("Your queue element is not able to empty")
} else {
varfirst++
if (varfirst > arraqueues.size - 1) {
System.out.println("So unable to remove the queue element: " + arraqueues[varfirst - 1])
varfirst = 0
} else {
System.out.println("So unable to remove the queue element: " + arraqueues[varfirst - 1])
}
latestsize--
}
}
fun peek():Int? {
if (isQueueEmpty()) {
println("Your queue element is not removed so queue memory is not empty")
return null
} else {
return arraqueues[varfirst]
}
}
fun size():Int {
return latestsize
}
private fun exam(): Boolean {
var status = false
if (latestsize === arraqueues.size) {
status = true
}
return status
}
fun isQueueEmpty(): Boolean {
var status = false
if (latestsize === 0) {
status = true
}
return status
}
private fun eample() {
val latestsize = this.arraqueues.size * 3
val latestArray = IntArray(latestsize)
var queuevalues = varfirst
var index = -1
while (true) {
latestArray[++index] = this.arraqueues[queuevalues]
queuevalues++
if (queuevalues == this.arraqueues.size) {
queuevalues = 0
}
if (latestsize === index + 1) {
break
}
}
this.arraqueues = latestArray
System.out.println("Have a Nice day users your latest queue size is: " + this.arraqueues.size)
this.varfirst = 0
this.varlast = index
}
}
fun main(args: Array<String>) {
val outs = SecondExam()
outs.enqueue(7)
println(outs.size())
outs.dequeue()
println(outs.size())
outs.enqueue(41)
println(outs.size())
outs.enqueue(56)
println(outs.size())
println(outs.dequeue())
println(outs.size())
println(outs.peek())
println(outs.size())
println(outs.isQueueEmpty())
}
Output:
In the second example, we performed queue operations by using the default methods.
Example #3
Code:
import java.util.PriorityQueue
fun main(args: Array<String>) {
val ints: PriorityQueue<String> = PriorityQueue<String>()
ints.add("SIva")
ints.add("Raman")
ints.add("Welcome To My Domain")
ints.add("Thank you users for spending the time with us")
println("Your total result is: " + ints.peek())
while (!ints.isEmpty()) {
println(ints.remove())
}
}
Output:
In the final example, we used the priority queue class and its methods for adding the elements in the queue.
Conclusion
In kotlin, the language queue is one of the collection interfaces, and it is used for to store and remove the datas using the FIFO concept. The datas are stored on the last side, that is, the rear position of the array index, and the datas are removed using the front position.
Recommended Articles
This is a guide to Kotlin Queue. Here we also discuss the introduction, syntax, and working of queues in kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –