Queue in Scala Last Updated : 15 Oct, 2019 Comments Improve Suggest changes Like Article Like Report A queue is a first-in, first-out (FIFO) data structure. Scala offers both an immutable queue and a mutable queue. A mutable queue can be updated or extended in place. It means one can change, add, or remove elements of a queue as a side effect. Immutable queue, by contrast, never change. In Scala, Queue is implemented as a pair of lists. One is used to insert the elements and second to contain deleted elements. Elements are added to the first list and removed from the second list. The two most basic operations of Queue are Enqueue and Dequeue. Enqueue - Adding an element at the end of the queue. Dequeue - Deleting an element from the beginning of the queue. Methods in Queue: +=: This method is used to add a single element in the end of the queue. ++=: This method is used to Insert more than one the element in the end of the queue. clear: Remove all elements from the queue. dequeue: Returns the first element in the queue enqueue: Adds all the elements to the queue. equals: Checks if two queues are structurally identical. front: Returns the first element in the queue. isEmpty: Check if the queue is empty or not. Below are simple Scala programs to demonstrate these operations: Example 1: Scala // Scala program for illustrating Queue // Import Queue import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Initialize a queue var q1 = Queue(1, 2, 3, 4, 5) // Print the elements of queue print("Queue Elements: ") q1.foreach((element:Int) => print(element+" ")) // Print the first element of the queue var firstElement = q1.front println("\nFirst element in the queue: "+ firstElement) // Enqueue 10 in the queue q1.enqueue(10) // Print the elements of queue print("Queue Elements after enqueue: ") q1.foreach((element:Int) => print(element+" ")) // Dequeue first element from the queue var deq = q1.dequeue // Print the elements of queue print("\nQueue Elements after dequeue: ") q1.foreach((element:Int) => print(element+" ")) // Print the Dequeued element print("\nDequeued element: " + deq) // using isEmpty method println("\nQueue is empty: "+ q1.isEmpty) } } Output: Queue Elements: 1 2 3 4 5 First element in the queue: 1 Queue Elements after enqueue: 1 2 3 4 5 10 Queue Elements after dequeue: 2 3 4 5 10 Dequeued element: 1 Queue is empty: false Example 2: Scala // Scala program for illustrating Queue // Import Queue import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Initialize a queue var fruits = Queue[String]() // Adding elements to the queue fruits.enqueue("apple") fruits.enqueue("banana") fruits.enqueue("mango") fruits.enqueue("guava") // Print the elements of queue print("Queue Elements: ") fruits.foreach((element:String) => print(element+" ")) // Print the first element of the queue var firstElement = fruits.front println("\nFirst element in the queue: "+ firstElement) // Enqueue pineapple in the queue fruits.enqueue("pineapple") // Print the elements of queue print("Queue Elements after enqueue: ") fruits.foreach((element:String) => print(element+" ")) // Dequeue first element from the queue var deq = fruits.dequeue // Print the elements of queue print("\nQueue Elements after dequeue: ") fruits.foreach((element:String) => print(element+" ")) // Print the Dequeued element print("\nDequeued element: " + deq) // Using clear method println("\nclear the queue: "+ fruits.clear) // Using isEmpty method println("\nqueue is empty: "+ fruits.isEmpty) } } Output: Queue Elements: apple banana mango guava First element in the queue: apple Queue Elements after enqueue: apple banana mango guava pineapple Queue Elements after dequeue: banana mango guava pineapple Dequeued element: apple clear the queue: () queue is empty:true Create Quiz Comment R rupesh_rao Follow 0 Improve R rupesh_rao Follow 0 Improve Article Tags : Scala Scala scala-collection Explore OverviewScala Programming Language3 min readIntroduction to Scala7 min readSetting up the environment in Scala3 min readHello World in Scala2 min readBasicsScala Keywords2 min readScala Identifiers3 min readData Types in Scala3 min readVariables in Scala3 min readControl StatementsScala | Decision Making (if, if-else, Nested if-else, if-else if)5 min readScala | Loops(while, do..while, for, nested loops)5 min readBreak statement in Scala3 min readScala | Literals4 min readOOP ConceptsClass and Object in Scala5 min readInheritance in Scala5 min readOperators in Scala11 min readScala Singleton and Companion Objects3 min readScala Constructors4 min readScala | Polymorphism5 min readScala | Multithreading3 min readScala this keyword2 min readMethodsScala | Functions - Basics3 min readAnonymous Functions in Scala2 min readScala | Closures3 min readRecursion in Scala4 min readMethod Overloading in Scala5 min readMethod Overriding in Scala8 min readLambda Expression in Scala4 min readScala Varargs2 min readStringsScala String4 min readScala | String Interpolation3 min readScala | StringContext2 min readRegular Expressions in Scala5 min readStringBuilder in Scala4 min readScala PackagesPackages In Scala4 min readScala | Package Objects3 min readChained Package Clauses in Scala3 min readFile Handling in Scala3 min readScala TraitScala | Traits7 min readScala | Sealed Trait4 min readScala | Trait Mixins3 min readTrait Linearization in Scala5 min readCollectionsScala Lists5 min readScala ListBuffer6 min readListSet in Scala6 min readScala Map5 min readScala | Arrays6 min readScala | ArrayBuffer4 min readScala | Tuple5 min readSet in Scala | Set-13 min readSet in Scala | Set-27 min readBitSet in Scala5 min readHashSet In Scala4 min readStack in Scala3 min readHashMap in Scala3 min readTreeSet in Scala4 min readIterators in Scala5 min readScala | Option3 min read Like