Open In App

Scala Map mkString() method with a separator with example

Improve
Improve
Like Article
Like
Save
Share
Report

This method is same as mkString() method but here a separator is also added.

Method Definition: def mkString(sep: String): String

Return Type: It returns the elements of the map as a string along with the separator between them.

Example #1:




// Scala program of mkString()
// method with a separator
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating map
        val m1 = Map("geeks" -> 5, "for" -> 3, "cs" -> 2)
          
        // Applying mkString method 
        val result = m1.mkString("/")
          
        // Displays output
        println(result)
    }
}


Output:

geeks -> 5/for -> 3/cs -> 2

Example #2:




// Scala program of mkString()
// method with a separator
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating map
        val m1 = Map("geeks" -> 5, "for" -> 3, "geeks" -> 5)
          
        // Applying mkString method 
        val result = m1.mkString("/")
          
        // Displays output
        println(result)
    }
}


Output:

geeks -> 5/for -> 3


Last Updated : 26 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads