EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Scala Queue

By Shalu SharmaShalu Sharma

Home » Software Development » Software Development Tutorials » Scala Tutorial » Scala Queue

Scala Queue

Definition of Scala Queue

Queue is a data structure and it follows FIFO. FIFO stands for first in first out. That means the elements insert first in the queue will be removed first. Scala supports two types of queue i.e. mutable and immutable. Immutable queues are those queues which once implemented cannot be changed. whereas mutable queue is different they can be extended further and we can update them as well.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

varqueue_name = Queue(values)

In the above syntax, we are assigning value to queue object. See below example to assign value to queue.

varmyQueue = Queue(100,200,400,600,700)

How Queue Works in Scala?

In scala, queue is also part of the collection and it provides us two types of queue i.e. mutable and immutable. But in the scala queue implement in the form of pair of the list. One list added the element into the list and the second list deletes the element from the list. Queue follows the FIFO approach that means first in and first out, the elements we add first to the queue will be removed first from the queue. The queue is also a collection that is used to store the element and retrieve the element.

Scala queue perform two operations;

  • Dequeue: Delete an element from the tail of the queue. (at the end)
  • Enqueue: Adding an element at the head of the queue. (at the start)

Scala queue extends and implements the following class and interfaces which are mentioned below:

  • ArrayDeque
  • IndexedSeqOps[A, Queue, Queue[A]]
  • StrictOptimizedSeqOps[A, Queue, Queue[A]]
  • IterableFactoryDefaults[A, Queue]
  • ArrayDequeOps[A, Queue, Queue[A]]
  • Cloneable[Queue[A]]
  • DefaultSerializable

Now let’s take a look at one simple program for beginners;

import scala.collection.immutable._
object Demo{
def main(args:Array[String]){
var myqueue1 = Queue(100, 200, 700, 100)
var myqueue2:Queue[Int] = Queue(400, 56, 900, 90, 425, 950)
println(  "here is the queue one"+myqueue1)
println( "here is the queue second" + myqueue2)
}
}

Here we need to import the package scala.collection.immutable._   to create the queue object. we are creating two queues with the specified value and in the second queue we are also defining the type of value it will take. In the second queue it will take Int only.

Methods of Scala Queue with Examples

Methods and examples of scala queue are given below:

1. ++:()

In method concatenate two queues with all the elements of the second queue will be added at the front of the first queue.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// queue creation
val queue1 = Queue(220, 330, 440, 550)
val queue2 = Queue(40, 60, 70, 30 )
// calling method
valfinalQueue = queue1.++:(queue2)
// output print
print("final queue is  ::"  + finalQueue)
}

Output:

Scala Queue-1.1

2. :+()

This method will add one element at the end of the queue and return a new queue.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// queue creation
val queue1 = Queue(220, 330, 440, 550)
valfinalResult = queue1.:+(2)
// output
print("final Result queue is ::" + finalResult)
}

Output:

Scala Queue-1.2

3.++=()

This method is used to add the element at the end of another queue.

Popular Course in this category
Scala Programming Training (3 Courses,1Project)3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity | 1 Project
4.5 (5,556 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)All in One Software Development Bundle (600+ Courses, 50+ projects)
Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
val queue2 = Queue(340, 20)
valfinalResult = queue2.++=(queue1)
// output
print("final Result queue is ::" + finalResult)
}

Output:

Scala Queue-1.3

4. map()

This method is used to perform some operation on the existing queue elements one by one and gives us the new queue.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before  ::" + queue1)
// map method
valfinalResult = queue1.map(x => x + 10)
// Display output
print("finalResult queue after modify elements :: " + finalResult)
}

Output:

Scala Queue-1.4

5. min()

This method is used to return the minimum value from the queue.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before  ::" + queue1)
// map method
valfinalResult = queue1.min
// Display output
print("minimum value from the queue is  :: " + finalResult)
}

Output:

Scala Queue-1.5

6. max()

This method is used to return the maximum value from the queue.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before  ::" + queue1)
// map method
valfinalResult = queue1.max
// Display output
print("maximum value from the queue is  :: " + finalResult)
}

Output:

Scala Queue-1.6

7. sum()

This method is used to sum all the elements present in the queue.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before  ::" + queue1)
// map method
valfinalResult = queue1.sum
// Display output
print("sum of all values in the queue is   :: " + finalResult)
}

Output:

Output-1.7

8. last()

This method is used to return the last elements present in the queue.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before  ::" + queue1)
// map method
valfinalResult = queue1.last
// Display output
print("last value in the queue is   :: " + finalResult)
}

Output:

Output-1.8

9. contains()

This method is used to check whether the element is present in the queue or not. It will return true or false based on the condition.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before  ::" + queue1)
// map method
valfinalResult = queue1.contains(200)
// Display output
print("to check 200 present in queue or not  ::"+ finalResult)
}

Output:

Output-1.9

10. list()

This method is used to return all the elements present in the queue as a list.

Example

import scala.collection.mutable._
object Main extends App{
// Your code here!
// creating queue
val queue1 = Queue(300, 500, 200, 100)
println("queue before  ::" + queue1)
// map method
valfinalResult = queue1.toList
// Display output
print("List containing all the queue elements are  ::"+ finalResult)
}

Output:

Output-1.10

Recommended Articles

This is a guide to Scala Queue. Here we also discuss the definition and how queue work in scala? along with methods and different examples. you may also have a look at the following articles to learn more –

  1. Scala Abstract Class
  2. Scala High Order Functions
  3. Scala for Loop
  4. Constructors in Scala 

Scala Programming Training (3 Courses)

3 Online Courses

9+ Hours

Verifiable Certificate of Completion

Lifetime Validity

1 Project

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Scala Tutorial
  • Advanced
    • Constructors in Scala
    • Scala Class Constructor
    • Scala Abstract Class
    • Scala String
    • Scala Collections
    • Scala High Order Functions
    • Scala Date Function
    • Scala Stream
    • Scala Class
    • Scala Function
    • Scala Closure
    • Scala Range
    • Scala Either
    • Scala Random
    • Scala Tuples
    • Scala Partial Function
    • Scala Split
    • Scala Finds
    • Scala List
    • Scala Trait
    • Scala Try Catch
    • Scala Regex
    • Scala Vector
    • Scala Seq
    • Scala Byte
    • Scala ListBuffer
    • Scala ArrayBuffer
    • Scala flatMap
    • Scala HashMap
    • Scala Map Function
    • Scala SortBy
    • Scala Some
    • Scala Generic
    • Scala getOrElse
    • Scala REPL
    • Scala Logging
    • Scala Lambda
    • Scala Queue
    • Scala Set
    • Scala Singleton
    • Scala groupBy
    • Scala reduce
    • Scala filter
    • Scala Option
    • Scala Native
    • Scala DataFrame
    • Scala Anonymous Function
    • Scala Read File
    • Scala Write to File
    • Scala Pattern Matching
    • Scala ClassTag
    • Scala Synchronized
    • Scala zipwithindex
    • Scala Interview Questions
  • Basics
    • What is Scala?
    • Uses of Scala
    • Scala Versions
    • Scala Data Types
    • Scala Companion Object
    • Scala Operators
    • Scala: The Functional Approach
    • Scala Object
    • Scala Implicit
    • Scala IDE
  • Control Flow
    • Scala if else
    • Break in Scala
    • Scala for Loop
    • Scala foreach
    • Scala While Loops

Related Courses

Scala Programming Training Course

Programming Language Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Scala Programming Training (3 Courses) Learn More