Open In App

Scala List ::() operator with example

Last Updated : 26 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The ::() operator in Scala is utilized to add an element to the beginning of the List.
Method Definition: def ::(x: A): List[A] Return Type: It returns the stated list after adding an element to the beginning of it.
Example #1: Scala
// Scala program of ::()
// method

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
    
        // Creating a list
        val m1 = List(1, 2, 3)
        
        // Applying :: operator
        val res = m1.::("5")
        
        // Displays output
        println(res)
    
    }
}
Output:
List(5, 1, 2, 3)
Example #2: Scala
// Scala program of ::()
// method

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
    
        // Creating a list
        val m1 = List(1, 2, 3)
        
        // Applying :: operator
        val res = m1.::("1")
        
        // Displays output
        println(res)
    
    }
}
Output:
List(1, 1, 2, 3)

Next Article

Similar Reads