Open In App

Scala Queue clear() method with example

Last Updated : 18 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The clear() method is utilized to delete all the elements of the queue.

Method Definition: def clear(): Unit

Return Type: It returns an empty queue.

Example #1:




// Scala program of clear() 
// method 
  
// Import Queue  
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a queue 
        val q1 = Queue(10, 11, 12, 13, 14)
          
          
        // Print the queue
        println("Before clear() method: " + q1)
          
        // Applying clear() method 
        q1.clear()
          
        // Display output
        print("After clear() method: " + q1)   
          
    


Output:

Before clear() method: Queue(10, 11, 12, 13, 14)
After clear() method: Queue()

Example #2:




// Scala program of clear() 
// method 
  
// Import Queue  
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a queue 
        val q1 = Queue("geeks", "for", "geeks")
          
          
        // Print the queue
        println("Before clear() method: " + q1)
          
        // Applying clear() method 
        q1.clear()
          
        // Display output
        print("After clear() method: " + q1)   
          
    


Output:

Before clear() method: Queue(geeks, for, geeks)
After clear() method: Queue()


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads