在这段代码中
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
...
}
我看到 print 会发现 questions created portfolio for 3455c2b9-37f2-4373-9dcd-9e71b43e8c3d 和 tagSet Set()。 tagSet 空假
为什么 tagSet 是 Set()
而它的空值为 false
?这不应该是真的吗?
最佳答案
可能集合中有一个空字符串""
所以打印的时候不可见,例如考虑
scala> Set("").toString
val res0: String = Set()
scala> Set("").isEmpty
val res1: Boolean = false
你可以确认是否是这样
tagSet.contains("")
作为旁注,请考虑 stringOf
如何打印它
scala> scala.runtime.ScalaRunTime.stringOf(Set(""))
val res0: String = Set("")
还有 PPrint将打印带有可见引号的字符串,例如
println(Set(""))
pprint.pprintln(Set(""))
println("")
pprint.pprintln("")
输出
Set()
Set("")
""
关于scala - isEmpty 为空集返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63086493/