How to use addString() in Scala Set collection

 65 total views,  3 views today

Welcome to Scala series. In this tutorial, you will learn How to use addString() in Scala Set collection.

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 use addString() in Scala Set collection.

Solution

addString() is the most useful method of a Scala Set which allows generating a string builder by appending elements of the collection. When addString() is applied on a Set or collection, it returns a StringBuilder by concatenating of all elements of the collection. With the help of the String Builder, we can create a String output or do some manipulation.

There are three variants of addString() operation

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

Let us see an example of each variant.

addString() – without separator example

This variant of addString() allows us to append all elements of the Set to a string builder.

/**
 * How to use addString() in Scala Set collection
 */
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

    //create a StringBuilder
    var builder = new StringBuilder()

    //Apply addString
    var newbuilder = set.addString(builder)

    //Print
    println(newbuilder.toString())
  }
}

Output

staticreferencelikesharefeedback

addString() – with separator example

This variant of addString() allows us to append all elements of the Set with a separator to a string builder.

/**
 * How to use addString() in Scala Set collection
 */
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

    //create a StringBuilder
    var builder = new StringBuilder()

    //Apply addString
    var newbuilder = set.addString(builder, "=")

    //Print
    println(newbuilder.toString())
  }
}

Output

staticreference=like=share=feedback

addString() with start,separator and end example

This variant of addString() allows us to append all elements of the Set with a separator and also with a start and end Strings to a string builder.

/**
 * How to use addString() in Scala Set collection
 */
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

    //create a StringBuilder
    var builder = new StringBuilder()

    //Apply addString
    var newbuilder = set.addString(builder, "Start-", "=", "-End")

    //Print
    println(newbuilder.toString())
  }
}

Output

Start-staticreference=like=share=feedback-End

ReferencesScala Set addString operation

I hope you like this tutorial and you were able to understand Scala Set addString 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!.