How to convert a Scala Set to String using mkString()

 71 total views,  2 views today

Welcome to Scala series. In this tutorial, you will learn How to convert a Scala Set to String using mkString()

Basics of Scala Set

  • Set is a collection to store values that don’t have/allow duplicate values. Always Set will have unique elements.
  • Set is Iterable and inherits all functionality of the iterable interface.
  • There are two variants of Set in Scala
    • Immutable Set(default)
    • Mutable Set
  • By default, Scala will use Immutable Set. In any case, if a mutable Set is needed, then we need to explicitly import mutable Set.
  • Immutable Set allows us to apply a function on Set but resulting in the creation of a new Set every time. It doesn’t allow us to change the source. On the other hand, the mutable Set allows modifying the source itself.

I have written a separate post on Scala immutable and mutable collection. So please read before you proceed.

Objective

How to convert a Scala Set to String using mkString. We will use mkString() to join/concat all elements to form a String.

Solution

mkString() is the most useful method of a Scala collection which allows generating a string based on the elements of the collection. When mkString() is applied on a collection, it returns a string by concatenation of all elements of the collection.

There are three variants of mkString() operation

  1. mkString() without separator.
  2. mkString() with separator.
  3. mkString() with start,separator and end.

Let us see an example of each variant.

mkString() – without separator example

This variant of mkString() operation allows us to displays all elements of the collection in a string.

/**
 * How to convert a Scala Set to String using mkString()
 */
package com.scala

import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //create a Set
    var set = Set("a", "b", "c", "d", "a", "c") //Without seperator

    //Apply mkString
    println(set.mkString)
  }
}

Output

abcd

mkString() – with separator example

This variant of mkString() operation allows us to displays all elements of the collection with a separator in a string.

/**
 * How to convert a Scala Set to String using mkString()
 */
package com.scala

import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //create a Set
    var set = Set("staticreference", "like", "share", "feedback") //WITH seperator

    //Apply mkString
    println(set.mkString("-"))
  }
}

Output

staticreference-like-share-feedback

mkString() – with start,separator and end example

This variant of mkString() operation allows us to displays all elements of the collection with a separator, start and end Strings in a string.

/**
 * How to convert a Scala Set to String using mkString()
 */
package com.scala

import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //create a Set
    var set = Set("staticreference", "like", "share", "feedback") //WITH seperator

    //Apply mkString
    println(set.mkString("/", "-", ";")) //With start,separator and end

  }
}

Output

/staticreference-like-share-feedback;

ReferencesScala Set mkString operation

I hope you like this tutorial and you were able to understand Scala Set mkString operation with examples.

Thanks for reading and please give us a thumb up and comment below.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.