-1

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?

yalkris
  • 2,596
  • 5
  • 31
  • 51

2 Answers2

3

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.

Tim
  • 26,753
  • 2
  • 16
  • 29
2

Adding parens at the end fixed it:

val mySet = mutable.HashSet[(String, String, String)]()
mySet += myTuple 
erip
  • 16,374
  • 11
  • 66
  • 121
yalkris
  • 2,596
  • 5
  • 31
  • 51