isEmpty returning false for empty set

Viewed 204

In this code

    def findQuestionCreatedCount(transaction:DistributedTransaction,userId:UUID,tagSet:Set[String]):Future[List[(String,Int)]]={
        logger.trace(s"will find questions created portfolio for ${userId} and tagSet ${tagSet}. tagSet empty ${tagSet.isEmpty}")
        if(tagSet.isEmpty == false) { //set has data
      ...
}

I see print will find questions created portfolio for 3455c2b9-37f2-4373-9dcd-9e71b43e8c3d and tagSet Set(). tagSet empty false

How come tagSet is Set() yet its empty value is false? Shouldn't it be true?

1 Answers

Possibly there is an empty string "" inside the set so it is not visible when printed, for example consider

scala> Set("").toString
val res0: String = Set()

scala> Set("").isEmpty
val res1: Boolean = false

You could confirm if that is the case like so

tagSet.contains("")

As a side note consider how stringOf prints it

scala> scala.runtime.ScalaRunTime.stringOf(Set(""))
val res0: String = Set("")

Also PPrint would print strings with surrounding quotes visible, for example

println(Set(""))
pprint.pprintln(Set(""))
println("")
pprint.pprintln("")

outputs

Set()
Set("")

""
Related