Search code examples
scalasetmutable

Add Tuple3 to a mutable Set in Scala


I have already looked at adding tuples to a Set in scala but nothing seems to work in my case

 val mySet = mutable.HashSet[(String, String, String)]
 val myTuple = ("hi", "hello", "there")

mySet ++= myTuple
mySet += myTuple  // Expects String instead of (String, String, String)
mySet :+ myTuple
mySet :: myTuple

Except the second rest of them are compiler errors. How can I add a Tuple to a mutable Set in scala?


Solution

  • I recommend using empty to create an empty collection:

    val mySet = mutable.HashSet.empty[(String, String, String)]
    

    This avoids the issue that you found, and makes the intent of the expression clear.