Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isEmpty returning false for empty set

Tags:

scala

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?

like image 817
Manu Chadha Avatar asked Mar 25 '26 12:03

Manu Chadha


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("")

""
like image 64
Mario Galic Avatar answered Mar 27 '26 03:03

Mario Galic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!