How to use a Queue in Scala (mutable, immutable)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.29, “How to Use a Queue in Scala”

Problem

You want to use a queue data structure in a Scala application.

Solution

A queue is a first-in, first-out (FIFO) data structure. Scala offers both an immutable queue and mutable queue. This recipe demonstrates the mutable queue.

You can create an empty, mutable queue of any data type:

import scala.collection.mutable.Queue
var ints = Queue[Int]()
var fruits = Queue[String]()
var q = Queue[Person]()

You can also create a queue with initial elements:

scala> val q = Queue(1, 2, 3)
q: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3)

Add elements with +=, ++=, enqueue

Once you have a mutable queue, add elements to it using +=, ++=, and enqueue, as shown in the following examples:

scala> import scala.collection.mutable.Queue
import scala.collection.mutable.Queue

// create an empty queue
scala> var q = new Queue[String]
q: scala.collection.mutable.Queue[String] = Queue()

// add elements to the queue in the usual ways
scala> var q = new Queue[String]
q: scala.collection.mutable.Queue[String] = Queue()

scala> q += "apple"
res0: scala.collection.mutable.Queue[String] = Queue(apple)

scala> q += ("kiwi", "banana")
res1: scala.collection.mutable.Queue[String] = Queue(apple, kiwi, banana)

scala> q ++= List("cherry", "coconut")
res2: scala.collection.mutable.Queue[String] = Queue(apple, kiwi, banana, cherry, coconut)

// can also use enqueue
scala> q.enqueue("pineapple")

scala> q
res3: scala.collection.mutable.Queue[String] = Queue(apple, kiwi, banana, cherry, coconut, pineapple)

dequeue

Because a queue is a FIFO, you typically remove elements from the head of the queue, one element at a time, using dequeue:

// take an element from the head of the queue
scala> val next = q.dequeue
next: String = apple

// 'apple' is removed from the queue
scala> q
res0: scala.collection.mutable.Queue[String] = Queue(kiwi, banana, cherry, coconut, pineapple)

// take the next element
scala> val next = q.dequeue
next: String = kiwi

// 'kiwi' is removed from the queue
scala> q
res1: scala.collection.mutable.Queue[String] = Queue(banana, cherry, coconut, pineapple)

dequeueFirst, dequeueAll

You can also use the dequeueFirst and dequeueAll methods to remove elements from the queue by specifying a predicate:

scala> q.dequeueFirst(_.startsWith("b"))
res2: Option[String] = Some(banana)

scala> q
res3: scala.collection.mutable.Queue[String] = Queue(cherry, coconut, pineapple)

scala> q.dequeueAll(_.length > 6)
res4: scala.collection.mutable.Seq[String] = ArrayBuffer(coconut, pineapple)
11.29. Using a Queue | 373

scala> q
res5: scala.collection.mutable.Queue[String] = Queue(cherry)

A Queue is a collection class that extends from Iterable and Traversable, so it has all the usual collection methods, including foreach, map, etc. See the Queue Scaladoc for more information.

See Also